从URI Android的映像文件的路径 [英] Android Image file path from URI

查看:148
本文介绍了从URI Android的映像文件的路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的活动清单code

 <活动机器人:名字=FileReceiver>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.SEND/>            <类机器人:名字=android.intent.category.DEFAULT/>            <数据机器人:mime类型=图像/ */>
        &所述; /意图滤光器>
        &所述;意图滤光器>
            <作用机器人:名字=android.intent.action.SEND_MULTIPLE/>            <类机器人:名字=android.intent.category.DEFAULT/>            <数据机器人:mime类型=图像/ */>
        &所述; /意图滤光器>
    < /活性GT;

我的活动code:

 意向书I = getIntent();
    字符串行动= i.getAction();
如果(action.equals(Intent.ACTION_SEND)){
        乌里imageUri =(URI)i.getParcelableExtra(Intent.EXTRA_STREAM);
        如果(imageUri!= NULL){
            Log.i(1234567,URI:+ imageUri);
            Log.i(123456,路径:+ imageUri.getPath());
            文件源= NULL;
            来源=新的文件(imageUri.getPath());
            串fileNme =/+ source.getName();            文件目的地=新的文件(Environment.getExternalStorageDirectory()getAbsolutePath()+路径);
            Log.i(123456,目标路径:+目的地);
            尝试{
                FileUtils.copyFile(源,目的);
            }赶上(IOException异常五){
                Log.i(123456,IO异常);
                e.printStackTrace();
            }        }

即时得到例外

  16 01-09:10:09.756:W / System.err的(30225):java.io.FileNotFoundException:源'内容:/媒体/外部/图像/媒体/ 127 ' 不存在

我如何获得图像的路径时,我收到DCIM文件夹的图像?


解决方案

  / **
 *辅助检索图像URI的路径
 * /
公共字符串的getPath(URI URI){
        //只是一些安全建
        如果(URI == NULL){
            // TODO执行一些记录或显示用户反馈
            返回null;
        }
        //首先尝试从媒体商店检索图像
        //这只会从图库中选择图片的工作
        的String [] =投影{MediaStore.Images.Media.DATA};
        光标光标= managedQuery(URI,投影,NULL,NULL,NULL);
        如果(指针!= NULL){
            INT与Column_Index =光标
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            返回cursor.getString(Column_Index中);
        }
        //这是我们的后备这里
        返回uri.getPath();
}

来源:
<一href=\"http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically\">Get/pick从图像的Andr​​oid内置的图库应用程序编程

My Manifest activity code

        <activity android:name="FileReceiver" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
        <intent-filter>
            <action android:name="android.intent.action.SEND_MULTIPLE" />

            <category android:name="android.intent.category.DEFAULT" />

            <data android:mimeType="image/*" />
        </intent-filter>
    </activity>

My Activity code :

Intent i = getIntent();
    String action = i.getAction();


if (action.equals(Intent.ACTION_SEND)) {
        Uri imageUri = (Uri) i.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            Log.i("1234567", "URI : " + imageUri);
            Log.i("123456", "path :" + imageUri.getPath());
            File source = null;
            source = new File(imageUri.getPath());
            String fileNme = "/" + source.getName();

            File destination = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + path);
            Log.i("123456", "destination path :" + destination);
            try {
                FileUtils.copyFile(source, destination);
            } catch (IOException e) {
                Log.i("123456", "IO Exception");
                e.printStackTrace();
            }

        }

im getting exception

01-09 16:10:09.756: W/System.err(30225): java.io.FileNotFoundException: Source 'content:/media/external/images/media/127' does not exist

how do i get the path of the image when i receive an image from DCIM folder ?

解决方案

/**
 * helper to retrieve the path of an image URI
 */
public String getPath(Uri uri) {
        // just some safety built in 
        if( uri == null ) {
            // TODO perform some logging or show user feedback
            return null;
        }
        // try to retrieve the image from the media store first
        // this will only work for images selected from gallery
        String[] projection = { MediaStore.Images.Media.DATA };
        Cursor cursor = managedQuery(uri, projection, null, null, null);
        if( cursor != null ){
            int column_index = cursor
            .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            return cursor.getString(column_index);
        }
        // this is our fallback here
        return uri.getPath();
}

source: Get/pick an image from Android's built-in Gallery app programmatically

这篇关于从URI Android的映像文件的路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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