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


相关推荐

  • HTML5移动端手机网站开发

    HTML5移动端手机网站开发手写手机网站一般我们自己手动开发手机网站的话,基本可以划分两类来做到。一类是通过在网页头部添加meta标签进行实现(网页指html5的格式来开发)。另一类是通过CSS3的Media标签(媒介查询)来实现。    在这里我们详细讲解下,利用添加meta标签来做手机网站。基本在网页头部我们只需添加四个meta标签就可以实现一个手机网站的框架。我一起来看看是哪些meta标签。1、添加viewport标签…

    2022年6月21日
    34
  • 机器人手眼标定详解

    机器人手眼标定详解手眼标定详解研究现状所谓手眼标定是统一视觉系统和机器人的坐标系,从而可以使视觉系统所确定的物体位姿可以转换到机器人坐标系下,由机械臂完成对物体的作业。最常见的手眼系统包括Eye-to-Hand和Eye-in-Hand两种。在Eye-to-Hand手眼系统中,摄像机与机器人基座的位置是相对固定的,手眼关系式求解摄像机坐标系与机器人基座坐标系之间的转换关系。在Eye-in-Hand手眼系统中,摄像机由于固定在机械臂末端,手眼关系求解的是摄像机坐标系与机械臂末端坐标系之间的转换关系。在机器人处于不同的位置和

    2022年5月2日
    32
  • iptable 详解_iptable命令详解1

    iptable 详解_iptable命令详解1-p-protocal[!]protocol:协议-s-source[!]address[/mask]:源地址-d–destination[!]address[/mask]:目的地址-j–jumptarget:-i–in-interface[!][name]:入口-o–out-interface[!][name]:出口-f,–fragment:分片指定-pt…

    2022年5月28日
    78
  • Java安全之挖掘回显链

    Java安全之挖掘回显链0x00前言前文中叙述反序列化回显只是为了拿到Request和Response对象。在这里说的的回显链其实就是通过一连串反射代码获取到该Request对象。在此之前想吹

    2021年12月13日
    51
  • notepad++编译C++

    notepad++编译C++

    2021年8月18日
    56
  • latex参考文献上标怎么打_latex的上下角标

    latex参考文献上标怎么打_latex的上下角标1、使用\supercite{}2、加入以下语句,引用时直接使用\cite{}原文链接:https://www.latexstudio.net/archives/7686.html\makeatletter\def\@cite#1#2{\textsuperscript{[{#1\if@tempswa,#2\fi}]}}\makeatother

    2025年10月10日
    4

发表回复

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

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