• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.example.android.apis.content;
18 
19 // Need the following import to get access to the app resources, since this
20 // class is in a sub-package.
21 import com.example.android.apis.R;
22 
23 import android.app.Activity;
24 import android.os.Bundle;
25 import android.widget.TextView;
26 
27 import java.io.IOException;
28 import java.io.InputStream;
29 
30 
31 /**
32  * Demonstration of styled text resources.
33  */
34 public class ReadAsset extends Activity
35 {
36     @Override
onCreate(Bundle savedInstanceState)37 	protected void onCreate(Bundle savedInstanceState)
38     {
39         super.onCreate(savedInstanceState);
40 
41         // See assets/res/any/layout/styled_text.xml for this
42         // view layout definition.
43         setContentView(R.layout.read_asset);
44 
45         // Programmatically load text from an asset and place it into the
46         // text view.  Note that the text we are loading is ASCII, so we
47         // need to convert it to UTF-16.
48         try {
49             InputStream is = getAssets().open("read_asset.txt");
50 
51             // We guarantee that the available method returns the total
52             // size of the asset...  of course, this does mean that a single
53             // asset can't be more than 2 gigs.
54             int size = is.available();
55 
56             // Read the entire asset into a local byte buffer.
57             byte[] buffer = new byte[size];
58             is.read(buffer);
59             is.close();
60 
61             // Convert the buffer into a string.
62             String text = new String(buffer);
63 
64             // Finally stick the string into the text view.
65             TextView tv = (TextView)findViewById(R.id.text);
66             tv.setText(text);
67         } catch (IOException e) {
68             // Should never happen!
69             throw new RuntimeException(e);
70         }
71     }
72 }
73 
74