Прежде всего, позвольте мне дать вам ссылку: AssetManager , с помощью этого класса мы можем легко получить доступ к любым файлам, лежащим в каталоге Assets приложения Android. (или любые подпапки в каталоге Assets).
Теперь мы можем получить объект класса AssetManager с помощью метода getAssets () :
|
1
|
AssetManager assetManager = getAssets(); |
И остальную часть процедуры, которую я дал и описал, комментируя в примере, так что теперь рассмотрим полные решения, представленные ниже, с привязкой к выходу.
Выход:
Решение:
ReadFileAssetsActivity.java
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
|
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. Это просто проблема внутри плагина кода.
|
01
02
03
04
05
06
07
08
09
10
11
12
13
14
|
<!--?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 — чтение файла из активов
Ссылка: Android — чтение файла из активов от нашего партнера JCG Пареш Н. Майяни в блоге TechnoTalkative .
