[英]Android app crashing in onActivityResult method
我的Android應用程序在情況2和情況3不起作用(不從相機拍照)時崩潰。 情況0和情況1運作良好。
onClick方法:
public void onClick(View v) {
if(position == 0 && menuID == mFrameID1)
{
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);//pick photo from gallery
}
else if(position == 1 && menuID == mFrameID1)
{
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 0);//capture from camera
}
else if(position == 0 && menuID == mFrameID2)
{
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 2);//pick photo from gallery
}
else if(position == 1 && menuID == mFrameID2)
{
Intent takePicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(takePicture, 3);//capture from camera
}
}
onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case 0:
if(resultCode == RESULT_OK){
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
mPhotoConteiner2.setImageBitmap(photo);
}
break;
case 1:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
mPhotoConteiner2.setImageURI(selectedImage);
}
break;
case 2:
if(resultCode == RESULT_OK){
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
mPhotoConteiner1.setImageBitmap(photo);
}
break;
case 3:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
mPhotoConteiner1.setImageURI(selectedImage);
}
break;
}
}
它在行崩潰
Bitmap photo = (Bitmap) imageReturnedIntent.getExtras().get("data");
在情況2
抱歉,我是android編程的新手。 並尋求幫助。
哦,問題是當我在錯誤的地方為案例2和案例3編寫代碼時。 我應該在case3中編寫case2代碼,在case2中編寫case3。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.