2012-02-16 49 views
1

我有一個Android應用程序正在使用一個外部的罐子,除了常規的類別HTML文件外,還有一個外部的罐子 。讀取APK內的文件

含義我最後的apk根目錄看起來是這樣的

  • 資產產生
  • 資源
  • AndroidManifest.xml中
  • classes.dex
  • resources.arsc
  • 的helloworld.html

如何從我的應用程序訪問最後一個文件 「helloworld.html」?

+2

可以保存資產裏面的html文件替換字符串「assets/SecureManifest.xml」,然後調用「getAssets( )「? – goodm 2012-02-16 09:34:49

回答

1

Android包層次結構不是像Java應用程序包一樣。 所以你不能像這樣訪問文件。

我認爲你必須在你的應用程序中使用這個helloworld.html文件。

所以把這個文件/asset目錄和你的活動代碼只是用

getAssets()獲取文件。

也獲得類似文件:file:///android_asset/helloworld.html

+0

這不是我想要的。我給開發者一個jar,我希望他們只是使用jar,而不是告訴他們 - 「另外,請把這個html文件放到你的資產目錄中」 – Omer 2012-02-16 10:03:12

+0

我的回答與你的問題有關。「我怎麼能從我的應用程序訪問最後一個文件「helloworld.html」?「 – user370305 2012-02-16 10:06:12

+0

對不起,我不清楚。 html文件不在assets目錄中 – Omer 2012-02-16 10:20:17

0

爲什麼不做出庫項目,而不是一個罐子裏的?

0

你必須與你的文件「helloworld.html

public static InputStream getInputStreamFromApkResource(String apkFilePath, String apkResPath) throws IOException { 
    JarFile jarFile = new JarFile(apkFilePath); 
    JarEntry jarEntry = jarFile.getJarEntry(apkResPath); 
    return jarFile.getInputStream(jarEntry); 
} 

// Example usage reading the file "SecureManifest.xml" under "assets" folder: 
File sdcard = Environment.getExternalStorageDirectory(); 
File apkFile = new File(sdcard, "file.apk"); 
if (apkFile.exists()) { 
    try { 
     InputStream is =getInputStreamFromApkResource(apkFile.toString(), "assets/SecureManifest.xml"); 
     BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
     String str; 
     while ((str = br.readLine()) != null) { 
      Log.d("***", str); 
     } 

    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

GitHub的要點可以發現here