【问题标题】:Convert base64 image data to an image file(.png) and save it to Local file system [closed]将base64图像数据转换为图像文件(.png)并将其保存到本地文件系统[关闭]
【发布时间】:2013-01-18 07:15:35
【问题描述】:

我有 base64 格式的图像数据,我想将此 base64 字符串转换为图像(.PNG)文件并将该文件保存到我的 android 应用程序中的本地文件系统。请为我提出一个解决方案

【问题讨论】:

    标签: android image base64


    【解决方案1】:

    试试这个。

    FileOutputStream fos = null;
    try {
        if (base64ImageData != null) {
           fos = context.openFileOutput("imageName.png", Context.MODE_PRIVATE);
           byte[] decodedString = android.util.Base64.decode(base64ImageData, android.util.Base64.DEFAULT);
           fos.write(decodedString);                        
           fos.flush();
           fos.close();             
        }
    
    } catch (Exception e) {
    
    } finally {
        if (fos != null) {
            fos = null;
        }
    }
    

    【讨论】:

    • 谢谢拉吉。它工作得很好
    【解决方案2】:

    要将其转换为图像文件,您可以使用此...

    byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
    

    并将其保存到文件系统,您可以使用:

    _bitmapScaled.compress(Bitmap.CompressFormat.PNG, 100, decodedString);
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "test.png")
    f.createNewFile();
    //write the bytes in file
    FileOutputStream fo = new FileOutputStream(f);
    fo.write(bytes.toByteArray());
    
    // remember close de FileOutput
    fo.close();
    

    【讨论】:

    • 他的意思是他想将实际文本保存为图像?将二进制转换的 ASCII 文本放入字节数组将无法实现。
    • @Misha Bhardwaj 什么是“字节”变量?
    猜你喜欢
    • 2021-10-29
    • 2011-01-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 1970-01-01
    相关资源
    最近更新 更多