简体   繁体   English

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

[英]Null Pointer Exception when passing Bitmap through Intent

I am trying to pass a BItmap[] from one activity to other using Intent.But, it is not getting passed,which in result is causing a Null Pointer Exception I guess. 我正在尝试使用Intent将BItmap []从一个活动传递到另一个活动。但是,它没有被传递,结果导致了Null Pointer Exception。 I am taking upto four images with the camera and storing them in a Bitmap[] ,which I am trying to pass to another activity.The logcat says the error is at line 94,which is where the passed Bitmap is used. 我正在用相机拍摄多达四个图像,并将它们存储在Bitmap []中,我试图将其传递给另一个活动。logcat表示错误在第94行,该行是使用传递的Bitmap的位置。 Here's the code (only the relevant parts). 这是代码(仅相关部分)。 Kindly help,Thanks in advance ! 请帮忙,谢谢!

Take Pic.java : 以Pic.java:

the onCreateMethod: onCreateMethod:

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

} }

The onActivity result for the four imageViews (which start the Camera activity) : 四个imageViews(启动Camera活动)的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);      
    }
}

The method to store the individual bitmaps in the array : 将单个位图存储在数组中的方法:

      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;
 }

And finally, the onClickListener for the button : 最后,按钮的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);
        }
    });

And in the other Class: 在另一类中:

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

and for the send email button : 并为发送电子邮件按钮:

       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;
 }

Finally, the logcat : 最后,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)

Do not pass bitmap array to another activity by intent. 不要通过意图将位图数组传递给另一个活动。 Bitmap maybe is big file.It will cause "TransactionTooLargeException" 位图可能是大文件。它将导致“ TransactionTooLargeException”

You have 2 ways to fix this : 您有2种方法可以解决此问题:

1: Use a global singleton(If bitmaps are temp data not exist in files) 1:使用全局单例(如果位图是临时数据,则文件中不存在)

2: Pass bitmap file path 2:传递位图文件路径

Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); Intent cameraIntent =新的Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); startActivityForResult(cameraIntent, CAMERA_REQUEST);; 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
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM