将结果ResultInfo {who = null,request = 0,result = -1,data = null}传递给活动失败

时间:2011-09-05 18:30:40

标签: android

  

可能重复:
  I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

我有一些代码。

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(CamDir,  filename);
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
startActivityForResult(intent, 0);


public void onActivityResult(int requestCode, int resultCode, Intent data) {
    Bitmap bitmap = null;
    if (resultCode == Activity.RESULT_OK && requestCode == 0) {
        Uri selectedImage = imageUri;
        ContentResolver cr = getContentResolver();
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, selectedImage);
    }

1.手机处于垂直位置。
2.启动申请。
3.按下按钮拍照 4.压好。 (保存照片)
一切都很好。

1.手机处于垂直位置。
2.启动申请。
3.按下按钮拍照 4.将手机旋转到水平位置 5.压好。 (保存照片)
有错误

E/AndroidRuntime(22779): java.lang.RuntimeException: Failure delivering result
ResultInfo{who=null, request=0, result=-1, data=null} to activity 
com.photo/com.photo.PhotoActivity}:
java.lang.NullPointerException

我认为当我将手机旋转到水平位置时,意图重新加载,而相机不是 知道在哪里发送结果。
如何解决这个问题。

解决方案:

onActivityResult(...){ 
... 
reload() 
} 
public void reload() 
{ Intent intent = getIntent(); overridePendingTransition(0, 0); 
intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION); 
finish(); 
overridePendingTransition(0, 0); 
startActivity(intent); 
}

1 个答案:

答案 0 :(得分:1)

public class BrowsePicture extends Activity {

private static final int SELECT_PICTURE = 1;

private String selectedImagePath;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ((Button) findViewById(R.id.Button01))
            .setOnClickListener(new OnClickListener() {

                public void onClick(View arg0) {

                    // in onCreate or any event where your want the user to
                    // select a file
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,
                            "Select Picture"), SELECT_PICTURE);
                }
            });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

}