I am developing an Android app. I keep a PDF data in MYSQL database as blob type. I am sending as base64 to Android app. How can I show the pdf in Android app?
Asked
Active
Viewed 7,135 times
1 Answers
1
As you have the yourBase64String with you, you can convert it to byte array and then save it as file.
FileOutputStream fos = null;
try {
if (yourBase64String != null) {
fos = context.openFileOutput("myPdf.pdf", Context.MODE_PRIVATE);
byte[] decodedString = android.util.Base64.decode(yourBase64String , android.util.Base64.DEFAULT);
fos.write(decodedString);
fos.flush();
fos.close();
}
} catch (Exception e) {
} finally {
if (fos != null) {
fos = null;
}
}
Now after this open this PDF file
Uri path = Uri.fromFile(new File(myPdf.pdf));
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(path, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
try {
startActivity(intent);
}
catch (ActivityNotFoundException e) {
Toast.makeText(OpenPdf.this,
"No Application Available to View PDF",
Toast.LENGTH_SHORT).show();
}
Add this permission in your manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Deˣ
- 4,191
- 15
- 24
-
Uri path = Uri.fromFile(file); where is should be file ? – Barış Sağlam Jun 17 '19 at 12:27
-
I received an error. file:///myPdf.pdf exposed beyond app through Intent.getData() – Barış Sağlam Jun 17 '19 at 12:49
-
add write external storage permission if not added – Deˣ Jun 17 '19 at 12:56
-
@BarışSağlam upvote the ansewr if helped and also accept as answer if it works for you. – Deˣ Jun 17 '19 at 13:01
-
I have not tried yet. I will write comment after try . – Barış Sağlam Jun 18 '19 at 13:39
-
@BarışSağlam Let me know if it worked or have any query. – Deˣ Jun 19 '19 at 10:33