2

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?

Barış Sağlam
  • 61
  • 2
  • 11

1 Answers1

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