Im using the following method to copy a PDF File from the assets folder to internal memory in android.I intent to open it using MUPDF Reader.As it does not support direct opening from assets folder im doing this.But there seems to be no answers on SO or anywhere to get the Internal Storage location in Android.I just need to open the copied file 'Sample.pdf' from the internal storage.Please help.
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(getExternalFilesDir(null), filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
} catch(IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}
I tried the method from your answer.I get the following
12-07 12:33:42.425: E/libmupdf(1858): Opening document...
12-07 12:33:42.427: E/libmupdf(1858): error: cannot open null//Sample.pdf
12-07 12:33:42.428: E/libmupdf(1858): error: cannot load document 'null//Sample.pdf'
12-07 12:33:42.429: E/libmupdf(1858): error: Cannot open document: 'null//Sample.pdf'
12-07 12:33:42.429: E/libmupdf(1858): Failed: Cannot open document: 'null//Sample.pdf'
12-07 12:33:42.433: I/System.out(1858): java.lang.Exception: Failed to open null//Sample.pdf