Failure delivering result ResultInfo{who=null, request=0, result=-1, data=null} to activity [duplicate]

匿名 (未验证) 提交于 2019-12-03 02:54:01

问题:

Possible Duplicate:
I'm getting a NullPointerException when I use ACTION_IMAGE_CAPTURE to take a picture

I have some code.

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.the phone in vertical position.
2.start the application.
3.press the button to take a photo.
4.press Ok. (save photo)
Everything fine.

1.the phone in vertical position.
2.start the application.
3.press the button to take a photo.
4.rotate the phone to horizontal position.
5.press Ok. (save photo)
Have error

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 

I think when I rotate the phone to horizontal position intent was reloaded, and camera not
know where to send results.
How to fix this problem.

Solution:

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:

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


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!