描述:
首先,让我给您一个链接: 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