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

Pra*_*ani 5 java android bitmap bitmapfactory

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

我正在使用此代码从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");
Run Code Online (Sandbox Code Playgroud)

和onPostExecute

if(jsondata!=null) {
    receiveData(jsondata);
}
Run Code Online (Sandbox Code Playgroud)

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);
Run Code Online (Sandbox Code Playgroud)

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

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

小智 6

从不同资源获取位图时,请尝试以下代码...

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);
Run Code Online (Sandbox Code Playgroud)

遵循此链接上的教程有效显示位图的方法