访问Asset文件夹
一、Asset文件夹的作用及特点
- 作用 :存储app资源文件,主要包括四种文件:文本文件、图像文件、网页文件(包括html中引用的js/ccs/jpg等资源)、音频视频文件
- 特点:assets目录下的文件在打包后会原封不动的保存在apk包中,不会被编译成二进制,assets目录下可以再建立文件夹,assets文件夹下的文件不会被映射到R.java中,访问的时候需要AssetManager类
二、创建asset文件夹及其子目录
创建asset文件夹——File->Folder->Assets Folder
创建子目录文件夹——选择asset文件夹,右键, New -> Directory
这里,我创建了两个文件夹,并放了一张照片和一个txt文件(里面存放了一首诗)
三、在MainActivity的布局文件下加入控件
1、创建一个shape文件,用于设置控件背景
ui_shape.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<stroke android:color="#5A5A5A"
android:width="1dp" />
</shape>
2、编辑MainActivity布局文件
acitvity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginVertical="10dp"
android:layout_marginHorizontal="10dp"
>
<Button
android:id="@+id/main_btn_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取txt文件"
android:textSize="20sp"
android:textAllCaps="false"
android:background="#2196F3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
<TextView
android:id="@+id/main_tv_txt"
android:layout_width="match_parent"
android:layout_height="150dp"
android:text="hello world \n 你好 "
android:textColor="#000"
android:textSize="20sp"
android:gravity="center"
android:background="@drawable/ui_shape"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"
/>
<Button
android:id="@+id/main_btn_img"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="读取图片文件"
android:textSize="20sp"
android:textAllCaps="false"
android:background="#2196F3"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginBottom="10dp"/>
<ImageView
android:id="@+id/main_im_img"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerInside"
android:padding="10dp"
android:background="@drawable/ui_shape"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
/>
</LinearLayout>
</ScrollView>
四、读取asset文件夹下的内容
public class MainActivity extends AppCompatActivity {
private Button btn_txt,btn_img;
private TextView tv_txt;
private ImageView im_img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getAssetsData("");
btn_txt = findViewById(R.id.main_btn_txt);
tv_txt = findViewById(R.id.main_tv_txt);
btn_txt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tv_txt.setText(getTxtFromAssets("txt/01.txt"));
}
});
btn_img = findViewById(R.id.main_btn_img);
im_img = findViewById(R.id.main_im_img);
btn_img.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
im_img.setImageBitmap(getImgFromAssets("image/icon1.jpg"));
}
});
}
// 从asset文件夹下读取txt文件的内容
private String getTxtFromAssets(String fileName) {
String result = "";
try {
InputStream is = getAssets().open(fileName);
int lenght = is.available();
byte[] buffer = new byte[lenght];
is.read(buffer);
result = new String(buffer, "utf8");
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
// 从asset文件夹下读取图片文件
private Bitmap getImgFromAssets(String fileName) {
Bitmap bitmap = null;
try {
InputStream is = getAssets().open(fileName);
bitmap = BitmapFactory.decodeStream(is);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
//读取 asset 文件夹下所有文件
private void getAssetsData(String fileName){
try {
String[] path = getAssets().list(fileName);
for(int i=0;i<path.length;i++){
Log.d("FileName",path[i]);
if(fileName.equals(""))
getAssetsData(path[i]);
else
getAssetsData(fileName+"/"+path[i]);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
五、实现效果
点击按钮后