2

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); 
}
Community
  • 1
  • 1
aspel
  • 71
  • 1
  • 2
  • 6
  • How did you solved your problem I'm facing the same problem now. – Pedro Teran Nov 04 '13 at 15:01
  • Try this: http://stackoverflow.com/questions/8248327/my-android-camera-uri-is-returning-a-null-value-but-the-samsung-fix-is-in-place – Bush Apr 26 '14 at 13:17
  • 1
    I had hoped the reload thing would work for me but it literally seems to reload the activity instead of doing something actually useful. It throws away the result of taking the picture too including any data I didn't explicitly persist and reload, plus it still crashes about half the time. – G_V Dec 17 '14 at 09:41

1 Answers1

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

}
Samir Mangroliya
  • 39,918
  • 16
  • 117
  • 134