0
votes

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())));
1
please post more code and your stacktrace of the errortyczj
wait I am doing upgradingCoas Mckey
again without showing us code where its crashing we dont know why its crashing. a guess would be your object that you store the uri in is destroyed when the device rotated so you need to save it in onSaveInstanceStatetyczj
I think you made the good guess , let me do this and would come back in a minuteCoas Mckey
well what would happen If I make my device orientation fix ,Coas Mckey

1 Answers

1
votes

It seems like that I am getting intent data as null or some times empty in both cases

If by "intent data as null", you mean that you are not getting a Uri passed to you via the Intent in onActivityResult(), that is correct. If you read the documentation for ACTION_IMAGE_CAPTURE, there is nothing in there that says that a camera app has to return a Uri pointing to anything.

Use MediaStore.EXTRA_OUTPUT, and remember the value that you provide. Then, use that to try to access the image:

package com.commonsware.android.camcon;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import java.io.File;

public class CameraContentDemoActivity extends Activity {
  private static final int CONTENT_REQUEST=1337;
  private File output=null;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent i=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File dir=
        Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

    output=new File(dir, "CameraContentDemo.jpeg");
    i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(output));

    startActivityForResult(i, CONTENT_REQUEST);
  }

  @Override
  protected void onActivityResult(int requestCode, int resultCode,
                                  Intent data) {
    if (requestCode == CONTENT_REQUEST) {
      if (resultCode == RESULT_OK) {
        Intent i=new Intent(Intent.ACTION_VIEW);

        i.setDataAndType(Uri.fromFile(output), "image/jpeg");
        startActivity(i);
        finish();
      }
    }
  }
}

Some camera apps are broken enough that even this will not work:

  • They may not truly honor ACTION_IMAGE_CAPTURE, despite having the <intent-filter> for it

  • They may ignore EXTRA_OUTPUT and write the photo where they want

Most camera apps behave well, but not all will. ACTION_IMAGE_CAPTURE is only suitable in cases where either you can live without getting a photo or you are willing to tell the user that their camera app appears to be poorly written and they should switch to something else.