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/118595.html原文链接:https://javaforall.net

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


相关推荐

  • SpringBoot解决文件上传,返回可访问路径

    SpringBoot解决文件上传,返回可访问路径问题描述:SpringBoot项目中需要上传文件到当前服务器的磁盘(即物理地址),返回可访问的路径给前端。前端利用路径显示文件内容。开发环境:SpringBoot2.0以上、JDK1.81.在springboot中加入下面代码 @Value(“${file.uploadFolder}”) privateStringuploadFolder; @Bean MultipartConf…

    2022年5月2日
    41
  • 数据中台怎么选型?终于有人讲明白了

    数据中台怎么选型?终于有人讲明白了数据中台怎么选型?终于有人讲明白了

    2022年6月13日
    29
  • Recyclerview 刷新「建议收藏」

    Recyclerview 刷新「建议收藏」前言:recyclerview比起listview功能上更加丰富外(如横向列表),在Item复用上也更加灵活,比如listview的某个Item数据需要更新,要通过notifyDataSetChanged方法对全部Item进行刷新,而recyclerview则可以精准刷新。介绍:(1)notifyItemChanged(position)只刷新该position的Item,即只是该Item调用onBindViewHolder,因此如果对数据源进行插、移除操作不能改方法只刷新操作的Item,毕竟该

    2025年5月31日
    0
  • Java解惑五:类之谜

    Java解惑五:类之谜

    2021年11月23日
    49
  • 实用的CSS3属性和使用技巧

    CSS可以改进网站的设计并且开拓网站设计更多的可能性,可以令你的网页更具吸引力。对于前端开发者、网站设计师来说,掌握并熟练应用CSS是一项必不可少的技能。下面列出了一些非常实用的CSS3属性和使用技巧

    2021年12月20日
    36
  • 对gmapping的理解

    对gmapping的理解参考网址:GMapping漫谈GMapping原理分析简单傻x的图解–gmapping

    2022年6月18日
    23

发表回复

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

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