Android –从Assets读取文件

描述:

首先,让我给您一个链接: AssetManager ,通过此类,我们可以轻松访问位于android应用程序的Assets目录中的任何文件。 (或Assets目录中的所有子文件夹)。

现在,我们可以使用getAssets()方法来拥有AssetManager类的对象:

AssetManager assetManager = getAssets();

通过在示例中进行注释,我已经给出并描述了其余过程,因此现在通过以下提供的完整解决方案进行输出快照。

输出:

解:

ReadFileAssetsActivity.java

package com.paresh.readfileasset;

import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.res.AssetManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author Paresh N. Mayani
 * @Website http://www.technotalkative.com
 */
public class ReadFileAssetsActivity extends Activity {

 /** Called when the activity is first created. */

 @Override
 public void onCreate(Bundle savedInstanceState) {

  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  TextView txtContent = (TextView) findViewById(R.id.txtContent);
  TextView txtFileName = (TextView) findViewById(R.id.txtFileName);
  ImageView imgAssets = (ImageView) findViewById(R.id.imgAssets);

  AssetManager assetManager = getAssets();

  // To get names of all files inside the "Files" folder
  try {
   String[] files = assetManager.list("Files");

   for(int i=0; i<files.length; i++)="" {="" txtfilename.append("\n="" file="" :"+i+"="" name=""> "+files[i]);
   }
  } catch (IOException e1) {
   // TODO Auto-generated catch block
   e1.printStackTrace();
  }

  // To load text file
        InputStream input;
  try {
   input = assetManager.open("helloworld.txt");

          int size = input.available();
          byte[] buffer = new byte[size];
          input.read(buffer);
          input.close();

          // byte buffer into a string
          String text = new String(buffer);

          txtContent.setText(text);
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

  // To load image
     try {
      // get input stream
      InputStream ims = assetManager.open("android_logo_small.jpg");

      // create drawable from stream
      Drawable d = Drawable.createFromStream(ims, null);

      // set the drawable to imageview
      imgAssets.setImageDrawable(d);
     }
     catch(IOException ex) {
      return;
     }
 }
}
</files.length;>

main.xml
注意:请考虑将scrollview视为ScrollView,将textview视为TextView….etc。 它只是代码插件内部的问题。

<!--?xml version="1.0" encoding="utf-8"?-->

<scrollview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent">

<linearlayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical">

    <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/txtContent">

 <imageview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/imgAssets">

  <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/txtFileName">
</textview></imageview></textview></linearlayout>

</scrollview>

从此处下载完整的源代码: Android –从Assets中读取文件

参考: Android –从我们的JCG合作伙伴的 Assets中读取文件  TechnoTalkative博客上的Paresh N. Mayani


翻译自: https://www.javacodegeeks.com/2012/02/android-read-file-from-assets.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值