通过Intent传递位图时出现空指针异常



我正试图使用Intent将BItmap[]从一个活动传递到另一个活动。但是,它没有被传递,这可能会导致Null指针异常。我用相机拍摄了四张图像,并将它们存储在位图[]中,我正试图将其传递给另一个活动。logcat表示,错误出现在第94行,即使用传递的位图的位置。这是代码(只有相关部分)。请帮忙,提前谢谢!

以Pic.java为例:

onCreateMethod:

         @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.takepic);
    initializeVar();
    storeInArray(images);

}

四个imageViews(用于启动相机活动)的onActivity结果:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == 1 && resultCode == RESULT_OK) {  
        photo1 = (Bitmap) data.getExtras().get("data"); 
        imageView1.setImageBitmap(photo1);
    } if(requestCode == 2 && resultCode == RESULT_OK) {
        photo2 = (Bitmap) data.getExtras().get("data");
        imageView2.setImageBitmap(photo2);
    } if(requestCode == 3 && resultCode == RESULT_OK) {
        photo3 = (Bitmap) data.getExtras().get("data");
        imageView3.setImageBitmap(photo3);
    } if(requestCode == 4 && resultCode == RESULT_OK) {
        photo4 = (Bitmap) data.getExtras().get("data");
        imageView4.setImageBitmap(photo4);      
    }
}

在数组中存储单个位图的方法:

      private Bitmap[] storeInArray(Bitmap[] bitmap) {
        // TODO Auto-generated method stub
     if(photo1 != null){
         bitmap[i]= photo1;
         i++;
    }if(photo2 != null){
        bitmap[i]= photo2;
         i++;
    }if(photo3 != null){
        bitmap[i]= photo3;
         i++;
    }if(photo4 != null){
        bitmap[i]= photo4;
         i++;
    }
    return bitmap;
 }

最后,按钮的onClickListener:

      bCamEmail.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent i = new Intent(TakePic.this,NewEmail.class);
            i.putExtra("Image",images);
            startActivity(i);
        }
    });

另一类:

       extras = getIntent().getExtras();
    if(extras!=null)
        receive = (Bitmap[]) extras.getParcelableArray("Image");

发送电子邮件按钮:

       newSend.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            convertEditTextToString();
            Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
            emailIntent.setType("text/plain");
            emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL,"seemaswain.09@gmail.com");
            emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,newSubject);
            emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,newContent);
            newUris = new ArrayList<Uri>();
            for(String file : images) {
                File fileIn = new File(file);
                Uri u = Uri.fromFile(fileIn);
                newUris.add(u);
            }
            emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, newUris);
            startActivity(emailIntent);
        }
    });

}
public String[] BitMapToString(Bitmap bitmap[]){
    int i=0;
     String[] temp= new String[2000];
    while(bitmap[i] !=null){
    ByteArrayOutputStream baos=new  ByteArrayOutputStream();
    bitmap[i].compress(Bitmap.CompressFormat.PNG,100, baos);
    byte [] b=baos.toByteArray();
    temp[i]=Base64.encodeToString(b, Base64.DEFAULT);
    i++;
    }
    return temp;
 }

最后,logcat:

     12-05 13:34:04.207: W/dalvikvm(3846): threadid=1: thread exiting with uncaught exception (group=0x40e62498)
     12-05 13:34:04.207: E/test(3846): Exception
     12-05 13:34:04.227: E/AndroidRuntime(3846): FATAL EXCEPTION: main
     12-05 13:34:04.227: E/AndroidRuntime(3846): java.lang.NullPointerException
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at com.example.bethechange.NewEmail$1.onClick(NewEmail.java:94)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.view.View.performClick(View.java:4106)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.view.View$PerformClick.run(View.java:17150)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.os.Handler.handleCallback(Handler.java:615)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.os.Handler.dispatchMessage(Handler.java:92)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.os.Looper.loop(Looper.java:137)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at android.app.ActivityThread.main(ActivityThread.java:4792)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at java.lang.reflect.Method.invokeNative(Native Method)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at java.lang.reflect.Method.invoke(Method.java:511)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
     12-05 13:34:04.227: E/AndroidRuntime(3846):    at dalvik.system.NativeStart.main(Native Method)

不要有意将位图数组传递给另一个活动。位图可能是大文件。它将导致"TransactionTooLargeException"

你有两种方法来解决这个问题:

1:使用全局单例(如果位图是临时数据,则文件中不存在)

2:传递位图文件路径

Intent cameraIntent=new Intent(android.product.MediaStore.ACTION_IMAGE_CAPTURE);startActivityForResult(cameraIntent,CAMERA_REQUEST);;

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_REQUEST) {
        try {
             photo = (Bitmap) data.getExtras().get("data");

        } catch (Exception e) {
            // TODO: handle exception
        }
    }
}

最新更新