zoukankan      html  css  js  c++  java
  • AssertManager的使用详解

    Android 系统为每个新设计的程序提供了/assets目录,这个目录保存的文件可以打包在程序里。

             /res 和/assets的不同点是:  android不为/assets下的文件生成ID。如果使用/assets下的文件,需要指定文件的路径和文件名。

     在文件中/assets 中建立/image子目录,image子目录下放入三个图片文件,其中第一个图片的名字是5429178。在/assets子目录中建立readme.txt文件,文件中输入文本“Hello world!”。

    xml文档:

     1 <?xml version="1.0" encoding="utf-8"?>
     2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     3     android:layout_width="fill_parent"
     4     android:layout_height="fill_parent"
     5     android:orientation="vertical" >
     6 
     7     <TextView
     8         android:layout_width="fill_parent"
     9         android:layout_height="wrap_content"
    10         android:text="1" />
    11 
    12     <EditText
    13         android:id="@+id/firstId"
    14         android:layout_width="fill_parent"
    15         android:layout_height="wrap_content"
    16         android:text="2" />
    17 
    18     <EditText
    19         android:id="@+id/secondId"
    20         android:layout_width="fill_parent"
    21         android:layout_height="wrap_content"
    22         android:text="3" />
    23 
    24 </LinearLayout>

    java文档:

     1 package com.example.bitmaptest;
     2 
     3 import java.io.ByteArrayOutputStream;
     4 import java.io.IOException;
     5 import java.io.InputStream;
     6 
     7 import android.app.Activity;
     8 import android.content.res.AssetManager;
     9 import android.os.Bundle;
    10 import android.util.Log;
    11 import android.view.Menu;
    12 import android.widget.EditText;
    13 
    14 public class BitmapTest extends Activity {
    15 
    16     private EditText firstField;
    17 
    18     private EditText secondField;
    19 
    20     @Override
    21     public void onCreate(Bundle savedInstanceState) {
    22 
    23         super.onCreate(savedInstanceState);
    24 
    25         // Log.d("show main.xml","ok ");
    26 
    27         setContentView(R.layout.activity_bitmap_test);
    28 
    29         Log.d("show main.xml", "ok");
    30 
    31         AssetManager assetManager = getAssets();
    32 
    33         String[] files = null;
    34 
    35         try {
    36 
    37             files = assetManager.list("image");
    38 
    39         } catch (IOException e) {
    40 
    41             Log.e("tag", e.getMessage());
    42 
    43         }
    44 
    45         firstField = (EditText) findViewById(R.id.firstId);
    46 
    47         firstField.setText(Integer.toString(files.length) + "file.File name is"
    48                 + files[0]);
    49 
    50         InputStream inputStream = null;
    51 
    52         try {
    53 
    54             inputStream = assetManager.open("readme.txt");
    55 
    56         } catch (IOException e) {
    57 
    58             Log.e("tag", e.getMessage());
    59 
    60         }
    61 
    62         String s = readTextFile(inputStream);
    63 
    64         secondField = (EditText) findViewById(R.id.secondId);
    65 
    66         secondField.setText(s);
    67 
    68     }
    69 
    70     private String readTextFile(InputStream inputStream) {
    71 
    72         ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    73 
    74         byte buf[] = new byte[1024];
    75 
    76         int len;
    77 
    78         try {
    79 
    80             while ((len = inputStream.read(buf)) != -1) {
    81 
    82                 outputStream.write(buf, 0, len);
    83 
    84             }
    85 
    86             outputStream.close();
    87 
    88             inputStream.close();
    89 
    90         } catch (IOException e) {
    91 
    92         }
    93 
    94         return outputStream.toString();
    95 
    96     }
    97 }
  • 相关阅读:
    单文档程序结构
    如何从一个对话框弹出单文档视图
    MFC 窗口居中显示 VS2010
    在单文档中显示我的第一个对话框
    在MFC中添加用户自定义消息
    MFC动态创建控件及添加消息响应
    MFC的DLL
    MFC多线程
    MFC的UDP编程实现
    MFC下CSocket编程详解
  • 原文地址:https://www.cnblogs.com/merryjd/p/2870028.html
Copyright © 2011-2022 走看看