Omer
Omer

Reputation: 335

Reading files inside a APK

I am having an android application that is using an external jar that has in addition to regular classes an html file.

Meaning my final apk root directory looks something like this

How can I access from my application to the last file "helloworld.html"?

Upvotes: 1

Views: 3800

Answers (3)

user1170873
user1170873

Reputation:

You have to replace the string "assets/SecureManifest.xml" with your file "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();
    }
}

The github gist can be found here

Upvotes: 0

Climbatize
Climbatize

Reputation: 1123

Why not making a library project instead of a jar?

Upvotes: 0

user370305
user370305

Reputation: 109257

Android package hierarchy is not a like java application package. So you can't access files like this.

I think you have to use this helloworld.html file in your application.

So put this file in /asset directory and in your activity code just get file using

getAssets().

also access file like: file:///android_asset/helloworld.html

Upvotes: 1

Related Questions