Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

android - Get Path of image from ACTION_IMAGE_CAPTURE Intent

Hi I am using ACTION_IMAGE_CAPTURE for capturing image using Intent as follows:

Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(
MediaStore.EXTRA_OUTPUT, (new File(Environment.getExternalStorageDirectory(),
String.valueOf(System.currentTimeMillis()) + ".jpg"))
);
startActivityForResult(cameraIntent, 0);

I need to store image in an sdcard and retrieve the path of that image using the onActivityResult method. I don't know how to get the image path of the currently captured image.

If any one knows please help.

Thanks

Question&Answers:os

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

1 Reply

0 votes
by (71.8m points)

This is how it works on 2.2 (different than on previous versions). When starting intent

        String fileName = "temp.jpg";  
        ContentValues values = new ContentValues();  
        values.put(MediaStore.Images.Media.TITLE, fileName);  
        mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  

        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
        intent.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);  
        startActivityForResult(intent, CAPTURE_PICTURE_INTENT);

you need to remember mCapturedImageURI.

When you capture image, in onActivityResult() use that URI to obtain file path:

            String[] projection = { MediaStore.Images.Media.DATA}; 
            Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
            int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
            cursor.moveToFirst(); 
            String capturedImageFilePath = cursor.getString(column_index_data);

UPDATE: On new Android devices you would not need MediaStore.EXTRA_OUTPUT, but you rather deduce image/video URI from data.getData() received from onActivityResult(..., Intent data), as nicely described under

Android ACTION_IMAGE_CAPTURE Intent

However, since this part is subject to manufacturer adaptation, you may still encounter phones where "old" approach may be useful.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...