java 图像不是使用 BitmapFactory.decodeByteArray 创建的

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14681643/
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-10-31 17:03:22  来源:igfitidea点击:

Image isn't creating using the BitmapFactory.decodeByteArray

javaandroidbitmapbitmapfactory

提问by Pragnani

Edit: When I save those bytes in the txt file and when I save it as png file , it shows the image, but it is not working here why...?

编辑:当我将这些字节保存在 txt 文件中并将其另存为 png 文件时,它会显示图像,但它在这里不起作用,为什么...?

I am using this code to create image from byte array on doInBackground()

我正在使用此代码从 doInBackground() 上的字节数组创建图像

String base64data=StringEscapeUtils.unescapeJava(IOUtils.toString(resp.getEntity().getContent()));
base64data=base64data.substring(1,base64data.length()-1);
JSONObject obj=new JSONObject(base64data);
JSONArray array=obj.getJSONArray("EMRTable");
JSONObject childobj=array.getJSONObject(0);
results=childobj.getString("DocumentInternalFormat");

and onPostExecute

和 onPostExecute

if(jsondata!=null) {
    receiveData(jsondata);
}

There is no error in the logcat, even there is no exception in it..but the image isn't showing. I have also did like this

logcat中没有错误,即使其中也没有异常......但图像没有显示。我也这样做过

String data=(String)object;
data=data.trim();
byte[] base64converted=Base64.decode(data,Base64.DEFAULT);          

ImageView image=new ImageView(context);
image.setImageBitmap(bmp);
setContentView(image);

but the result same image isn't showing but there is no exception or an error, what is the problem...

但结果相同的图像没有显示但没有异常或错误,有什么问题......

The commented lines are when I try to store those bytes into text file and when I pull the file, it shows the images with windows default image viewer.

注释行是当我尝试将这些字节存储到文本文件中时,当我拉出文件时,它会显示带有 Windows 默认图像查看器的图像。

回答by

Try this code while getting bitmap from different resources...

在从不同资源获取位图时尝试使用此代码...

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(base64converted,0,base64converted.length,options);

// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, 500, 500);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
Bitmap bmp1=BitmapFactory.decodeByteArray(base64converted,0,base64converted.length,options);

follow the tutorial on this link Efficient way to show bitmaps

按照此链接上的教程显示位图的有效方法

回答by V.J.

remove the below line from your code and try again

从您的代码中删除以下行并重试

base64data=base64data.substring(1,base64data.length()-1);