1

我正在尝试将图像从字节设置为位图并将其设置为 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)
4

1 回答 1

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. 还有一件事还记得UI不可能在线程背景中改变
 Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray , 0, byteArray .length);
 iv.setImageBitmap(bitmap );
于 2013-03-06T12:52:41.967 回答