MobileMon
MobileMon

Reputation: 8651

Android - Display PDF from server

Trying to display a PDF received from server but getting "Media not found error"

First I convert String to byte array:

byte[] data = Base64.decode(stringData, Base64.DEFAULT);

Then I write the byte[] to a file:

String path = getFilesDir() + "/myfile.pdf";     
File file = new File(path);

if (!file.exists()) {
    file.createNewFile();
}

FileOutputStream stream = new FileOutputStream(file);
stream.write(article.getFileDataBytes());
stream.close();

Then I try to display PDF:

Uri path2 = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path2, "application/pdf");
startActivity(intent);

But after calling startActivity I receive the "Media not found error"

I'm not sure what's wrong no exceptions are thrown when writing the file

Upvotes: 0

Views: 722

Answers (1)

CommonsWare
CommonsWare

Reputation: 1006584

You are writing the file to internal storage. No third-party apps have access to your internal storage directly. Use FileProvider to make the PDF available to third-party apps.

This sample app does pretty much what you need, except that I get the starting PDF from an asset.

Upvotes: 1

Related Questions