I have used android native camera app in my app many times. But it was before the Lollipop release. Now I am using the camera and getting crash my app.
It seems like that I am getting intent data as null or some times empty in both cases My crashes. I am talking about the following piece of code
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) { .....
here the intent is always null some of sites and question here suggested to stop using the following line of code while calling the camera app;
intent.putExtra(MediaStore.EXTRA_OUTPUT, xyz);
so using MediaStore.EXTRA_OUTPUT would not return the data intent. So after removing this line I was able to find the intent which is not null and empty
but still the app was crashing as the type of intent and the path has been changed. I do not understand now How to extract the uri of picture from the intent of the onActivityResult. Some one has stated that in android 5.0 now the camera image is also treated as the document. But I am not sure.
On official developer site I have seen that they are saying to use Corp function But I dont know How to use it. so in the end I am clue less.
Can some one please tell me that what should I implement to get the uri of image after saving it in gallery from the camera in the Android 5.0 Lollipop ?.
Based On CommansWare Answer
I have implemeneted some part of your code with mine and its started to work but it is working in lollipop only when the camera is Portrait but in land scap mode My app got crash and here is a stack trace , run time exception
java.lang.RuntimeException: Unable to resume activity {com.example.myapp/com.example.mapp.MyCameraActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=null} to activity {com.example.myapp/com.example.myapp.MyCameraActivity}: java.lang.NullPointerException: file at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3394)
Now I do not under stand that why it got crashed when the camera was in landscape mode.
Please suggest me a full source code or atleast the part next to onActivityResult
Edited
here is how on button click I am opening a camera
File output;
public void onLargeImageCapture(View v) {
mHighQualityImageUri = generateTimeStampPhotoFileUri();
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
File dir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
output=new File(dir, System.currentTimeMillis()
+ ".jpg");
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));
startActivityForResult(intent, REQUEST_CODE_HIGH_QUALITY_IMAGE);
}
private Uri generateTimeStampPhotoFileUri() {
Uri photoFileUri = null;
File outputDir=
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
// File outputDir = getPhotoDirectory();
if (outputDir != null) {
Time t = new Time();
t.setToNow();
File photoFile = new File(outputDir, System.currentTimeMillis()
+ ".jpg");
photoFileUri = Uri.fromFile(photoFile);
}
return photoFileUri;
}
and here is how I have implemented part for android version kit kat and lollipop
Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case REQUEST_CODE_HIGH_QUALITY_IMAGE:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri =Uri.fromFile(output) ; //out is your output file
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
// sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, mHighQualityImageUri));
//refreshing gallery
Intent mediaScanIntentKitKat = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntentKitKat.setData(Uri.fromFile(output));
sendBroadcast( mediaScanIntentKitKat);
Intent intentActivityKitKat = new Intent(MyCameraActivity.this, PhotoSortrActivity.class);
intentActivityKitKat.putExtra("data", Uri.fromFile(output));
Log.v("Uri before Sending", mHighQualityImageUri + "");
startActivity(intentActivityKitKat);
} else {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
onSaveInstanceState
– tyczj