在安卓系统中,BitmapFactory.decodeByteArray 返回null

4
在我的应用程序中,我正在将base64字符串转换为图像。为此,我首先将base64文件转换为字节数组,然后尝试将其转换为图像。
要将其转换为图像,我使用以下代码:
    File sdImageMainDirectory = new File("/data/data/com.ayansys.Base64trial");
    FileOutputStream fileOutputStream = null;
    String nameFile="Images";
    try {

        BitmapFactory.Options options=new BitmapFactory.Options();
        options.inSampleSize = 5;
        options.inDither = true; 
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);


        fileOutputStream = new FileOutputStream(
                sdImageMainDirectory.toString() +"/" + nameFile + ".jpg");


        BufferedOutputStream bos = new BufferedOutputStream(
                fileOutputStream);

        myImage.compress(CompressFormat.JPEG, quality, bos);

        bos.flush();
        bos.close();

但是我在这里获取的图像为空

Bitmap myImage = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);

请告诉我您宝贵的建议。

提前感谢您:)


只是一个提示:在你的代码前后留出空行以启用正确的代码格式化器。 - WarrenFaith
@Remmyabhavan 你好,我遇到了同样的问题,你能给我提些建议吗? - Pragnani
2个回答

2

decodeByteArray不会将base64编码转换为byte数组。您需要先使用Base64转换器将其转换为byte数组。


1
是的,我正在使用它将字节数组转换为位图图像。但是我遇到了空指针错误。我传递的ImageData是字节数组。 - Remmyabhavan

1
我遇到了同样的问题。我通过删除字符串中的以下部分来解决它:
"data:image/jpeg;base64,"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接