Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
533 views
in Technique[技术] by (71.8m points)

android - How to get path of picture in onActivityresult (Intent data is null)

I have to launch the camera, and when the users has done the picture, I have to take it and show it in a view.

Looking at http://developer.android.com/guide/topics/media/camera.html I have done:

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        bLaunchCamera = (Button) findViewById(R.id.launchCamera);
        bLaunchCamera.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Log.d(TAG, "lanzando camara");

                //create intent to launch camera
                Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

                imageUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); //create a file to save the image
                intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); //set the image file name

                //start camera
                startActivityForResult(intent, CAMERA_REQUEST);
            }
        });

/**
     * Create a File Uri for saving image (can be sued to save video to)
     **/
    private Uri getOutputMediaFileUri(int mediaTypeImage) {
        return Uri.fromFile(getOutputMediaFile(mediaTypeImage));
    }

    /**
     * Create a File  for saving image (can be sued to save video to)
     **/
    private File getOutputMediaFile(int mediaType) {
        //To be safe, is necessary to check if SDCard is mounted

        File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                (String) getResources().getText(R.string.app_name));

        //create the storage directory if it does not exist
        if (!mediaStorageDir.exists()) {
            if (!mediaStorageDir.mkdirs()) {
                Log.d(TAG, "failed to create directory");
                return null;
            }
        }

        //Create a media file name

        String timeStamp = new SimpleDateFormat("ddMMyyyy_HHmmss").format(new Date());
        File mediaFile;

        if (mediaType == MEDIA_TYPE_IMAGE) {
            mediaFile = new File(mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");

        } else {
            return null;
        }
        return mediaFile;
    }

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode == CAMERA_REQUEST) {
            if(resultCode == RESULT_OK) {
                //Image captured and saved to fileUri specified in Intent
                Toast.makeText(this, "image saved to:
" + data.getData(), Toast.LENGTH_LONG).show();
                Log.d(TAG, "lanzando camara");
            } else if(resultCode == RESULT_CANCELED) {
                //user cancelled the image capture;
                Log.d(TAG, "usuario a cancelado la captura");
            } else {
                //image capture failed, advise user;
                Log.d(TAG, "algo a fallado");
            }
        }
    }

When the picture has been done, the app crashes when it try to send the 'Toast' info because 'data' is null.

But if I debug the app I can see that the image has been saved.

enter image description here

So my question is: How can I get the path in the onActivityResult?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Amigo,

The problem you are facing is that, whenever we select an image from camera intent, it may finish the activity which called it, so imageUri object you created will be null while you return.

so you need to save it when you exit the activity (to go to camera intent), like this -

    /**
     * Here we store the file url as it will be null after returning from camera
     * app
     */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        // save file url in bundle as it will be null on screen orientation
        // changes
        outState.putParcelable("file_uri", mImageUri);
    }

and retrieve it back when you come back to the activity -

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);

        // get the file url
        mImageUri = savedInstanceState.getParcelable("file_uri");
    }

Your code looks fine, you only need to add this change in it.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...