android讀寫assets目錄下面的資源文件(文件夾)

android讀寫assets目錄下面的資源文件(文件夾)

    Android除了提供/res目錄存放資源文件外,在/assets目錄也會提供存放資源文件,在/assets目錄下面不會在R.java裏面自動生成ID,因此讀取assets目錄下面的資源文件須要提供路徑,咱們能夠經過AssetManager類來訪問這些文件。
    做者須要實現從  A.apk(  資源apk  ,把全部的資源如:so、apk、可執行文件等放到assets目錄下面,apk沒有實現邏輯代碼)拷貝資源到指定目錄下,因此做者建立了一個實現資源拷貝邏輯的  B.apk(  一個Service,也可用Activity實現),因爲拷貝路徑通常狀況下是不可訪問或者建立的(每一個apk安裝以後只能訪問/data/data/本身包名/下面的私有空間),做者須要這個apk可以獲取系統權限(System權限),則必須在AndroidManifest.xml聲明shareduserid,具體如何操做下一節進行記錄。 


1、AssetManager讀取文件經常使用的幾個API html

    1.文件讀取方式
    AssetManager.open(String filename),返回的是一個InputSteam類型的字節流,這裏的filename必須是文件,而不能是文件夾,AssetManager打開資源文件的open方法是一個重載方法,能夠添加一個打開方式的int參數,根據參數不一樣可作相應操做。具體請看官方文檔http://web.mit.edu/clio/MacData/afs/sipb/project/android/docs/reference/android/content/res/AssetManager.html
    2.資源文件是能夠存在文件夾以及子目錄
    public final String[]list(String path),返回當前目錄下面的全部文件以及子目錄的名稱。能夠經過遞歸遍歷整個文件目錄,實現全部資源文件的訪問。String[] Array of strings, one for each asset. These file names are relative to 'path'. You can open the file by concatenating 'path' and a name in the returned string (via File) and passing that to open(). java

2、相關實現代碼
資源APK(A.apk)


具體實現代碼片斷,因爲使用系統權限,生成的路徑能夠本身改一下B.apk
android

01 public void onCreate(Bundle savedInstanceState) {
02         super.onCreate(savedInstanceState);
03         setContentView(R.layout.main);
04         try {
05             ctxDealFile = this.createPackageContext("com.zlc.ipanel",
06                     Context.CONTEXT_IGNORE_SECURITY);
07         } catch (NameNotFoundException e1) {
08             // TODO Auto-generated catch block
09             e1.printStackTrace();
10         }
11      
12         btn3.setOnClickListener(new OnClickListener() {
13             @Override
14             public void onClick(View v) {
15                 // TODO Auto-generated method stub
16                 try {
17                     String uiFileName = "ipanelJoin";
18                     deepFile(ctxDealFile, uiFileName);
19                 } catch (Exception e) {
20                     // TODO Auto-generated catch block
21                     e.printStackTrace();
22                     textView.setText("file is wrong");
23                 }
24             }
25         });
26         //
27     }
28     public void deepFile(Context ctxDealFile, String path) {
29         try {
30             String str[] = ctxDealFile.getAssets().list(path);
31             if (str.length > 0) {//若是是目錄
32                 File file = new File("/data/" + path);
33                 file.mkdirs();
34                 for (String string : str) {
35                     path = path + "/" + string;
36                     System.out.println("zhoulc:\t" + path);
37                     // textView.setText(textView.getText()+"\t"+path+"\t");
38                     deepFile(ctxDealFile, path);
39                     path = path.substring(0, path.lastIndexOf('/'));
40                 }
41             } else {//若是是文件
42                 InputStream is = ctxDealFile.getAssets().open(path);
43                 FileOutputStream fos = new FileOutputStream(new File("/data/"
44                         + path));
45                 byte[] buffer = new byte[1024];
46                 int count = 0;
47                 while (true) {
48                     count++;
49                     int len = is.read(buffer);
50                     if (len == -1) {
51                         break;
52                     }
53                     fos.write(buffer, 0, len);
54                 }
55                 is.close();
56                 fos.close();
57             }
58         } catch (IOException e) {
59             // TODO Auto-generated catch block
60             e.printStackTrace();
61         }
62     }
相關文章
相關標籤/搜索