簡體   English   中英

通過Intent傳遞位圖時出現空指針異常

[英]Null Pointer Exception when passing Bitmap through Intent

我正在嘗試使用Intent將BItmap []從一個活動傳遞到另一個活動。但是,它沒有被傳遞,結果導致了Null Pointer Exception。 我正在用相機拍攝多達四個圖像,並將它們存儲在Bitmap []中,我試圖將其傳遞給另一個活動。logcat表示錯誤在第94行,該行是使用傳遞的Bitmap的位置。 這是代碼(僅相關部分)。 請幫忙,謝謝!

以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(啟動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);      
    }
}

將單個位圖存儲在數組中的方法:

      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”

您有2種方法可以解決此問題:

1:使用全局單例(如果位圖是臨時數據,則文件中不存在)

2:傳遞位圖文件路徑

Intent cameraIntent =新的Intent(android.provider.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
        }

    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM