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


相关推荐

  • Oracle 1521端口

    Oracle 1521端口当windows系统开启防火墙时,在win7中,要开启进的1521端口XP下,用telnet远程IP端口号(1521)来查看端口是否打开例:telnet192.168.1.101521win7下,netstat-naptcp来查看netstat-naptcpTCP192.168.1.10:1521192.168.1.225:3…

    2022年5月15日
    76
  • 二叉树(2)之二叉树的基本操作(遍历,找节点个数)

    二叉树(2)之二叉树的基本操作(遍历,找节点个数)

    2021年9月28日
    55
  • gimp中文版教程_GIMP中详细教程.pdf「建议收藏」

    gimp中文版教程_GIMP中详细教程.pdf「建议收藏」GIMP中详细教程GIMP实用系列教程1文件的打开和存储概述打开GIMP软件其初始界面如下:左边是工具,工具箱中每选择一种工具后,通常在其下部会出现一个与其相配的选项栏一起使用的。因此每选好一种工具,首先要把选项栏中的有关选项根据需要选定以后才开始使用。例如:图中选择了画笔,则画笔的选项栏可以选择其不透明度、画笔的笔尖形状、画笔的大小等选项。右边通常可以放置一个图层对话框,如未出现可以在下拉…

    2022年4月19日
    94
  • 手写数字识别数据集_卷积神经网络分类

    手写数字识别数据集_卷积神经网络分类基于卷积神经网络的手写数字识别(附数据集+代码)配置环境1.前言2.问题描述3.解决方案4.实现步骤4.1数据集选择4.2构建网络4.3训练网络4.4测试网络4.5图像预处理4.6传入网络进行计算5.代码实现5.1文件说明5.2使用方法5.3训练模型5.4配置环境使用环境:python3.8平台:Windows10IDE:PyCharm1.前言手写数字识别,作为机器视觉入门项目,无论是基于传统的OpenCV方法还是基于目前火热的深度学习、神经网络的方法都有这不错的训练效果。当然,这个项目也常常

    2025年11月18日
    3
  • 常用乘法公式_初中乘法公式有哪些

    常用乘法公式_初中乘法公式有哪些1、平方差公式$$a^2b^2=(a+b)(ab)$$2、完全平方公式$$(a±b)^2=a^2±2ab+b^2$$3、完全立方公式$$(a±b)^3=a^

    2022年8月4日
    5
  • DDL和DML的含义

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

    2022年4月6日
    57

发表回复

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

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