如何从相机刚刚拍摄获得的图像路径 [英] How to get Image Path just captured from camera

查看:125
本文介绍了如何从相机刚刚拍摄获得的图像路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code,但没有给我onActivity结果图片路径

 乌里selectedImageUri = data.getData();
                selectedImagePath = getPath(selectedImageUri);
                Log.w(周杰伦,摄像机的图像路径​​:+ selectedImagePath);

                Toast.makeText(MiscansOther_pannel.this,selectedImagePath,
                        Toast.LENGTH_LONG).show();
 

解决方案

这对我的作品......

code:

 乌里selectedImageUri = data.getData();
selectedImagePath = getRealPathFromURI(selectedImageUri);
 

方法: getRealPathFromURI()

  // ------------------------------------ ----
    / **
     *此方法用于获取文件的真实路径从URI
     *
     * @参数contentUri
     * @返回字符串
     * /
    // ----------------------------------------
    公共字符串getRealPathFromURI(URI contentUri)
    {
        尝试
        {
            的String []凸出= {MediaStore.Images.Media.DATA};
            光标光标= managedQuery(contentUri,凸出,NULL,NULL,NULL);
            INT与Column_Index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            返回cursor.getString(Column_Index中);
        }
        赶上(例外五)
        {
            返回contentUri.getPath();
        }
    }
 

编辑:

正如我注意到,在某些设备拍摄的图像后, onActivityResult数据()

因此​​,另一种方法,通过特定的图像文件名作为参数传递给你的意图捕捉图像 putExtra 参数。

然后还插入这个开放的形象在媒体商店,现在使用这种开放的我们为您进一步的使用,

您可以检查图像是否被捕获或不 File.exist()

code样子,

  ContentValues​​值=新ContentValues​​();
values​​.put(MediaStore.Images.Media.TITLE,图像文件名);
乌里mCapturedImageURI = getContentResolver()插入(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,值)。
意图int​​entPicture =新的意图(MediaStore.ACTION_IM​​AGE_CAPTURE);
intentPicture.putExtra(MediaStore.EXTRA_OUTPUT,mCapturedImageURI);
startActivityForResult(intentPicture,ACTION_TAKE_PICTURE);
 

现在,您可以用同样的方法从乌里获​​取文件的路径,

在这种情况下,它会在 onActivityResult()

  selectedImagePath = getRealPathFromURI(mCapturedImageURI); //不使用data.getData(),因为它返回null在一些设备,而不是使用mCapturedImageUR URI变量静态在code,
 

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();

解决方案

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,

这篇关于如何从相机刚刚拍摄获得的图像路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆