Java 如何解码base64字符串并将其转换为PDF/JPG并将其保存在存储中

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/36578863/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 18:27:20  来源:igfitidea点击:

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

javaandroidbase64fileoutputstream

提问by Edoardo Goffredo

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).

我想解码 base64 字符串并将其转换为文件(如 PDF/JPG)并将其保存到设备,例如如何在(/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.

现在,我想解码这个 base64 字符串并将其转换为文件并将其保存在设备上。

采纳答案by Prerak Sola

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
  • filepath:新文件的路径
  • base64String:您要转换的 base64 字符串

回答by jagadishlakkurcom jagadishlakk

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

在清单中授予 READ_STORAGE 和 Write_STORAGE 权限,不要忘记运行时权限。

 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);

}