AssetManager asset的使用

AssetManager asset的使用

大家好,又见面了,我是全栈君,祝每个程序员都可以多学几门语言。

 

Android 系统为每一个新设计的程序提供了/assets文件夹,这个文件夹保存的文件能够打包在程序里。/res 和/assets的不同点是,android不为/assets下的文件生成ID。假设使用/assets下的文件,须要指定文件的路径和文件名称。以下这个样例,显示怎样訪问/assets下的内容。

   在文件里/assets 中建立/image子文件夹,将/res/drawable下的icon.png子文件夹复制到该文件夹中。在/assets子文件夹中建立readme.txt文件,文件里输入文本“hello,world!!!”。

布局文件:main.xml

<?xml version=“1.0” encoding=“utf-8” ?>

<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”

    android:orientation=“vertical”

    android:layout_width=“fill_parent”

    android:layout_height=“fill_parent”

    >

<TextView 

    android:layout_width=“fill_parent”

    android:layout_height=“wrap_content”

    android:text=“@string/hello”

    />

    <EditText android:id=“@+id/firstId”

     android:layout_width=“fill_parent”

    android:layout_height=“wrap_content”

    android:text=“@string/hello”

    />

    <EditText android:id=“@+id/secondId”

     android:layout_width=“fill_parent”

    android:layout_height=“wrap_content”

    android:text=“@string/hello”

    />

 

</LinearLayout>

 

程序文件:

package com.cn.getassets;

 

import android.app.Activity;

import android.os.Bundle;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

import android.app.Activity ;

import android.content.res.AssetManager;

import android.os.Bundle ;

import android.util.Log;

import android.widget.EditText;

 

 

 

public class GetAssets extends Activity {

 private EditText firstField;

 private EditText secondField;

 @Override

 public void onCreate(Bundle savedInstanceState) {

  super .onCreate(savedInstanceState);

//  Log.d(“show main.xml”,”ok “);

  setContentView(R.layout.main );

  Log.d (“show main.xml”,”ok”);

  AssetManager assetManager = getAssets();

  String[] files = null ;

  try {

   files = assetManager.list(“image”);

  } catch (IOException e) {

   Log.e (“tag”, e.getMessage());

  }

  firstField = (EditText) findViewById(R.id.firstId );

  firstField.setText(Integer.toString (files.length)+”file.File name is”+ files[0]);

  InputStream inputStream = null ;

  try {

   inputStream = assetManager.open(“readme.txt”);

  } catch (IOException e) {

   Log.e (“tag”, e.getMessage());

  }

  String s = readTextFile(inputStream);

  secondField = (EditText) findViewById(R.id.secondId );

  secondField.setText(s);

 }

 

 private String readTextFile(InputStream inputStream) {

  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

  byte buf[] = new byte [1024];

  int len;

  try {

   while ((len = inputStream.read(buf)) != -1) {

    outputStream.write(buf, 0, len);

   }

   outputStream.close();

   inputStream.close();

  } catch (IOException e) {

  }

  return outputStream.toString();

 }

}

程序显示结果:使用模拟器。

http://blog.sina.com.cn/s/blog_6cf0d3f30100m2x6.html

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/118407.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • pycharm选哪个版本_pycharm版本区别

    pycharm选哪个版本_pycharm版本区别在pycharm中点击file-&gt;setting-&gt;project-&gt;projectInterpreter。 在出现的projectInterpreter右边的齿轮形状的图标点击add如下图,可以选择自己的版本

    2022年8月26日
  • android一键换机功能实现,不同品牌手机一键换机教程「建议收藏」

    android一键换机功能实现,不同品牌手机一键换机教程「建议收藏」我们正处于一个数码产品更新换代非常快速的时代,很多消费者会紧跟时代潮流,经常更换新手机,但是更换手机之后我们通常需要将旧手机里的数据进行转移,不同品牌之间的手机怎么进行一键换机呢?以安卓手机更换苹果手机转移数据为例:1、首先我们需要在安卓手机上安装“转移到iOS”APP,通过该应用我们可以将安卓手机的数据转移到新的苹果手机上2、确保安卓设备处于WiFi状态3、将安卓设备以及苹果设备连接电源4、设置…

    2022年5月9日
  • excel中多条件查找_多条件查找用什么函数

    excel中多条件查找_多条件查找用什么函数如下图所示,要求根据设备分类和品牌来查找相应的销售数量。1.使用VLOOKUP+辅助列进行多条件查找本例采用的方法是在原表的最前面加一辅助列,辅助列的公式为:=B2&C2然后再采用VL

    2022年8月1日
  • Android退出APP 并杀掉相关的所有进程

    Android退出APP 并杀掉相关的所有进程代码如下:ActivityManagermActivityManager=(ActivityManager)AppApplication.getInstance().getSystemService(Context.ACTIVITY_SERVICE);List<ActivityManager.RunningAppProcessInfo>m…

    2022年7月17日
  • DDL和DML的含义

    DDL表示DataDefinitionLanguage数据定义语言,主要包括CREATE,ALTER,DROP;隐性提交的,不能rollback。DML表示DataManipulationLanguage数据操作语言,主要的DML有SELECT,INSERT,UPDATE,DELETE;可以手动控制事务的开启、提交和回滚的。…

    2022年4月6日
  • 解读:百度框计算

    解读:百度框计算

    2022年3月11日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号