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)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 数据绑定中的一个问题”pages enableEventValidation=”true””[通俗易懂]

    数据绑定中的一个问题”pages enableEventValidation=”true””[通俗易懂]我们在用vs2005做数据绑定的时候运行出来的回发或回调参数无效。在配置中使用<pagesenableEventValidation=”true”/>或在页面中使用<%@PageEnableEventValidation=”true”%>启用了事件验证。出于安全目的,此功能验证回发或回调事件的参数是否来源于最初呈现这些事件的服务器控件。如果数据…

    2022年7月14日
    34
  • Nginx配置Https并进行Http强制跳转Https

    Nginx配置Https并进行Http强制跳转Https

    2021年5月29日
    156
  • pythonrequests代理ip_python使用requests模块使用ip代理池

    pythonrequests代理ip_python使用requests模块使用ip代理池importjsonimporttelnetlibimportrequestsimportrandom#代理ip列表proxy_url=”https://raw.githubusercontent.com/fate0/proxylist/master/proxy.list”#写入可用ip代理池文件路径ip_pool_file=”verified_proxies.json”#用…

    2022年5月26日
    80
  • UIAutomator2的使用教程

    UIAutomator2的使用教程uiautomator2是一个python库,用于Android的UI自动化测试,其底层基于Googleuiautomator,Google提供的uiautomator库可以获取屏幕上任意一个APP的任意一个控件属性,并对其进行任意操作。

    2022年7月21日
    59
  • ViewStub使用[通俗易懂]

    ViewStub使用[通俗易懂]一、ViewStub是什么?<ViewStub>标签实质上是一个宽高都为0的不可见的轻量级View。通过延迟按需加载布局的方式提升页面加载速度。二、ViewStub使用场景某布局默认是不可见,当满足特定场景才显示。比如网络异常提示、引导页等。三、ViewStub怎么使用?1、创建布局文件layout_test.xml(注:根标签可以是布局或控件,但不能为<merge>,子标签可以使用<merge>)<TextView…

    2022年6月28日
    28
  • linux go环境搭建_golang后端框架

    linux go环境搭建_golang后端框架1.下载go语言包,go1.9.2.linux-amd64.tar.gzhttps://www.golangtc.com/download2.解压安装[root@localhostlocal]#pwd/usr/local[root@localhostlocal]#tar-xzvfgo1.9.2.linux-amd64.tar.gz [root@localhostlocal]#c…

    2022年10月10日
    0

发表回复

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

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