How to solve DATA null issue that occured when capturing image through camera
public class ImageCaptureActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Uri mImageCaptureUri = Uri.fromFile(new File(
"/sdcard/gift2.JPG"));
Intent intent = new Intent(
"android.media.action.IMAGE_CAPTURE");
intent.putExtra(
android.provider.MediaStore.EXTRA_OUTPUT,
mImageCaptureUri);
startActivityForResult(intent, 0);
}
protected void onActivityResult(int requestCode,
int resultCode, Intent data) {
if (requestCode == 0
&& resultCode == Activity.RESULT_OK) {
Toast.makeText(getBaseContext(), "ImageCaptured",
Toast.LENGTH_LONG).show();
Uri chosenImageUri = data.getData()开发者_运维技巧;
Bitmap mBitmap = null;
try {
mBitmap = Media.getBitmap(
this.getContentResolver(),
chosenImageUri);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ImageView img = new ImageView(this);
img.setImageBitmap(mBitmap);
setContentView(img);
}
}
When I execute this class . After capturing image through camera and clicking "ok" I am getting null pointer exception at the statement "Uri chosenImageUri = data.getData();"
Can anyone help me in sorting out this issue. Thanks in Advance.I had the same problem. Well, maybe it's not the same source but I had the same result.
The source of this problem was that Android was loosing track of the URI because when the "android.media.action.IMAGE_CAPTURE" Activity started (when you start to see what the camera sees), I changed the orientation of the phone to LANDSCAPE... (witch is the default camera shooting orientation - and I'm guessing our AVD is simulating the Landscape too). Yeah, that was the source of quite a headache!
If I kept the phone in PORTRAIT to take my picture, the bug did not show (and I had a valid URI ^^).
So the simple fix was to add the following code in the Manifest:
android:configChanges="keyboardHidden|orientation"
Pasted it for the Activity that launched the "android.media.action.IMAGE_CAPTURE" Activity (in your case "ImageCaptureActivity").
That fixed it (and I hope it does for you - if it's not too late)!
Yes, I confirm data Intent
is always null
here with my app. However I don't know why.. Just a statement of fact.
Since you pass an image path to Camera, then in onActivityResult()
you can read/access the file for the passed path. You just need to keep that path in your ImageCaptureActivity
. Hopefully you know how to keep an Activity
state.
when you pass Uri (I mean path to create image from camera), by saying
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,mImageCaptureUri);
then you must be careful that, this returns null data @ activityresult ! so you wont get anything in data. Rather you will be able to access the image directly by the path you provided while creation of it.
Refer: Android Camera : data intent returns null
I am getting null pointer exception at the statement "Uri chosenImageUri = data.getData();"
This is probably because data is null and you are trying to call one of it's methods.
Verify that an object is non-null before you call it's methods.
精彩评论