字节数组到位图图像android

时间:2013-03-06 12:28:18

标签: android bytearray android-imageview bitmapimage

我正在尝试将图像从字节设置为位图并将其设置为imageView,但每当我将其设置为imageView时,我都会收到空指针异常,对不起,我只是新问题使用字节和位图

以下是代码:

iv = (ImageView)findViewById(R.id.imageView1);
    fileObject.getDataInBackground(new GetDataCallback() {
                    public void done(byte[] data, ParseException e) {
                      if (e == null) {
                        // use data for something
                        Bitmap bmp=BitmapFactory.decodeByteArray(data,0,data.length);
                        Log.d("test", " "+bmp.toString());
                        iv.setImageBitmap(bmp); <----- here getting error
                      } else {
                        Log.d("test", "There was a problem downloading the data.");
                      }
                    }
                  });

iv在课堂上在当地宣布。 在此先感谢您的帮助。

这是我要求的logcat:

03-06 20:15:46.417: E/AndroidRuntime(388): FATAL EXCEPTION: main
03-06 20:15:46.417: E/AndroidRuntime(388): java.lang.NullPointerException
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.shop.browser.ItemInfoActivity$1$1.done(ItemInfoActivity.java:115)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.parse.ParseFile$2.done(ParseFile.java:396)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.parse.GetDataCallback.internalDone(GetDataCallback.java:21)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.parse.GetDataCallback.internalDone(GetDataCallback.java:1)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.parse.BackgroundTask.onPostExecute(BackgroundTask.java:50)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.parse.BackgroundTask.onPostExecute(BackgroundTask.java:1)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.os.AsyncTask.finish(AsyncTask.java:417)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.os.Looper.loop(Looper.java:123)
03-06 20:15:46.417: E/AndroidRuntime(388):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-06 20:15:46.417: E/AndroidRuntime(388):  at java.lang.reflect.Method.invokeNative(Native Method)
03-06 20:15:46.417: E/AndroidRuntime(388):  at java.lang.reflect.Method.invoke(Method.java:521)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-06 20:15:46.417: E/AndroidRuntime(388):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-06 20:15:46.417: E/AndroidRuntime(388):  at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

Bitmap bmp = intent.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
  
      
  1. 在使用上面的代码之前检查你的byteArray大小,如果他们有这样的解码图像。
  2.   
  3. 还有一件事还记得在线程背景中无法更改UI
  4.   
 Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
 iv.setImageBitmap(bitmap );