In my applications i am having few pdf stored in my assests folder.Now what i want is to copy those pdfs to interal storage (private) asin (com.android.mypackage).I have written the code but seems not working.I am getting data for path not found
Code
private void CopyReadAssets() {
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = getDir("Mcq Demo", Context.MODE_PRIVATE);
try {
in = assetManager.open("Geography1.pdf");
out = new BufferedOutputStream(new FileOutputStream(file));
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
Intent intent = new Intent(Intent.ACTION_VIEW);
File mcqDemo = new File(file, "Geography1.pdf");
intent.setDataAndType(
Uri.parse("file://" + mcqDemo),
"application/pdf");
startActivity(intent);
}
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);
}
}
First, you are trying to write a PDF to a directory. That will not work. If you want to write a PDF to a file named
Geography1.pdf
, you need to do so explicitly, with aFileOutputStream
pointing to such a file.Second, your
Intent
will not work, as third-party apps have no access to your internal storage.