2012-02-16 108 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