Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to decode base64 string and convert it into PDF/JPG and save it in storage

I would like to decode a base64 string and turn it into a file (as PDF/JPG) and save it to device, how for example in (/storage/emulated/0/Download/file.pdf).

For encode a file I use this code:

File originalFile = new File("/mnt/sdcard/download/file.pdf");
    String encodedBase64 = null;
    try {
        FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
        byte[] bytes = new byte[(int) originalFile.length()];
        fileInputStreamReader.read(bytes);
        encodedBase64=Base64.encodeToString(bytes,Base64.NO_WRAP);
        messaggio=encodedBase64.toString();
        //encodedBase64 = new String(Base64.encode(bytes));
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

Now, I would like to decode this base64 string and convert it into a file and save it on the device.

like image 589
Edoardo Goffredo Avatar asked Apr 12 '16 16:04

Edoardo Goffredo


People also ask

How do I save a Base64 as a PDF?

Paste your string in the “Base64” field. Press the “Decode Base64 to PDF” button. Click on the filename link to download the PDF.

How do I decode Base64 text files?

To decode a file with contents that are base64 encoded, you simply provide the path of the file with the --decode flag. As with encoding files, the output will be a very long string of the original file. You may want to output stdout directly to a file.

Can we save Base64 string to database?

Generally no. Base64 will occupy more space than necessary to store those images. Depending on the size of your images and how frequently they're accessed, it is generally not an optimal stress to put on the database. You should store them on the file system or in something like Azure blob storage.


2 Answers

You can try this:

FileOutputStream fos = new FileOutputStream(filepath);
fos.write(Base64.decode(base64String, Base64.NO_WRAP));
fos.close();

Where:

  • filepath: Path to the new file
  • base64String: Your base64 string that you want to convert
like image 85
Prerak Sola Avatar answered Sep 22 '22 16:09

Prerak Sola


Give READ_STORAGE and Write_STORAGE Permission in Manifest and dont forget for RunTime Permission.

 public static void storetoPdfandOpen(Context context, String base) {



    String root = Environment.getExternalStorageDirectory().toString();

    Log.d("ResponseEnv",root);

    File myDir = new File(root + "/WorkBox");
    if (!myDir.exists()) {
        myDir.mkdirs();
    }

    Random generator = new Random();
    int n = 10000;
    n = generator.nextInt(n);

    String fname = "Attachments-" + n + ".pdf";
    File file = new File(myDir, fname);
    if (file.exists())
        file.delete();
    try {

        FileOutputStream out = new FileOutputStream(file);
        byte[] pdfAsBytes = Base64.decode(base, 0);
        out.write(pdfAsBytes);
        out.flush();
        out.close();


    } catch (Exception e) {
        e.printStackTrace();
    }

    File dir = new File(Environment.getExternalStorageDirectory(), "WorkBox");
    File imgFile = new File(dir, fname);
    Intent sendIntent = new Intent(Intent.ACTION_VIEW);

    Uri uri;
    if (Build.VERSION.SDK_INT < 24) {
        uri = Uri.fromFile(file);
    } else {
        uri = Uri.parse("file://" + imgFile); // My work-around for new SDKs, causes ActivityNotFoundException in API 10.
    }

    sendIntent.setDataAndType(uri, "application/pdf");
    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    sendIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(sendIntent);

}
like image 45
jagadishlakkurcom jagadishlakk Avatar answered Sep 24 '22 16:09

jagadishlakkurcom jagadishlakk