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
231 views
in Technique[技术] by (71.8m points)

android - How to get Image Path just captured from camera

below is my code but is not give me image path in onActivity result

Uri selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Log.w("jay", "Camera Image Path :" + selectedImagePath);

                Toast.makeText(MiscansOther_pannel.this, selectedImagePath,
                        Toast.LENGTH_LONG).show();
See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

This works for me...

Code:

Uri selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);

Method: getRealPathFromURI()

//----------------------------------------
    /**
     * This method is used to get real path of file from from uri
     * 
     * @param contentUri
     * @return String
     */
    //----------------------------------------
    public String getRealPathFromURI(Uri contentUri)
    {
        try
        {
            String[] proj = {MediaStore.Images.Media.DATA};
            Cursor cursor = managedQuery(contentUri, proj, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        catch (Exception e)
        {
            return contentUri.getPath();
        }
    }

EDIT:

As I noticed in some device after captured image the data in onActivityResult() is null,

So the alternate way, Pass the specific image filename as a argument to your Intent for capture image as putExtra parameter.

Then also insert this image Uri in Media Store, Now use this Uri for your further use,

You can check whether image is captured or not by File.exist(),

Code looks like,

ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, "Image File name");
Uri mCapturedImageURI = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);

Now, you can use the same method for get file path from Uri,

in this case it will be in onActivityResult(),

selectedImagePath = getRealPathFromURI(mCapturedImageURI); // don't use data.getData() as it return null in some device instead  use mCapturedImageUR uri variable statically in your code,

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

...