关于Android4.4的图片路径获取,如果回来的Uri的格式有两种

关于Android4.4的图片路径获取,如果回来的Uri的格式有两种
1、content://com.android.providers.media.documents/document/image:3951
2、content://media/external/images/media/3951

解决办法:
    1)、
        //>=4.4    时
        if(DocumentsContract.isDocumentUri(context, contentUri)){
            String wholeID = DocumentsContract.getDocumentId(contentUri);
            String id = wholeID.split(:)[1];
            String[] column = { MediaStore.Images.Media.DATA };
            String sel = MediaStore.Images.Media._ID + =?;
            Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column,
                    sel, new String[] { id }, null);
            int columnIndex = cursor.getColumnIndex(column[0]);
            if (cursor.moveToFirst()) {
                filePath = cursor.getString(columnIndex);
            }
            cursor.close();
        
        //<4.4时
        }else{
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = context.getContentResolver().query(contentUri, projection, null, null, null);
            int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            cursor.moveToFirst();
            filePath = cursor.getString(column_index);
        }
    2)、
       public static String getPath(final Context context, final Uri uri) {  
            
            final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;  
          
            // DocumentProvider  
            if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {  
                // ExternalStorageProvider  
                if (isExternalStorageDocument(uri)) {  
                    final String docId = DocumentsContract.getDocumentId(uri);  
                    final String[] split = docId.split(":");  
                    final String type = split[0];  
          
                    if ("primary".equalsIgnoreCase(type)) {  
                        return Environment.getExternalStorageDirectory() + "/" + split[1];  
                    }  
          
                    // TODO handle non-primary volumes  
                }  
                // DownloadsProvider  
                else if (isDownloadsDocument(uri)) {  
          
                    final String id = DocumentsContract.getDocumentId(uri);  
                    final Uri contentUri = ContentUris.withAppendedId(  
                            Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));  
          
                    return getDataColumn(context, contentUri, null, null);  
                }  
                // MediaProvider  
                else if (isMediaDocument(uri)) {  
                    final String docId = DocumentsContract.getDocumentId(uri);  
                    final String[] split = docId.split(":");  
                    final String type = split[0];  
          
                    Uri contentUri = null;  
                    if ("image".equals(type)) {  
                        contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;  
                    } else if ("video".equals(type)) {  
                        contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;  
                    } else if ("audio".equals(type)) {  
                        contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;  
                    }  
          
                    final String selection = "_id=?";  
                    final String[] selectionArgs = new String[] {  
                            split[1]  
                    };  
          
                    return getDataColumn(context, contentUri, selection, selectionArgs);  
                }  
            }  
            // MediaStore (and general)  
            else if ("content".equalsIgnoreCase(uri.getScheme())) {  
          
                // Return the remote address  
                if (isGooglePhotosUri(uri))  
                    return uri.getLastPathSegment();  
          
                return getDataColumn(context, uri, null, null);  
            }  
            // File  
            else if ("file".equalsIgnoreCase(uri.getScheme())) {  
                return uri.getPath();  
            }  
          
            return null;  
        }  
          
        /**
         * Get the value of the data column for this Uri. This is useful for
         * MediaStore Uris, and other file-based ContentProviders.
         *
         * @param context The context.
         * @param uri The Uri to query.
         * @param selection (Optional) Filter used in the query.
         * @param selectionArgs (Optional) Selection arguments used in the query.
         * @return The value of the _data column, which is typically a file path.
         */  
        public static String getDataColumn(Context context, Uri uri, String selection,  
                String[] selectionArgs) {  
          
            Cursor cursor = null;  
            final String column = "_data";  
            final String[] projection = {  
                    column  
            };  
          
            try {  
                cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,  
                        null);  
                if (cursor != null && cursor.moveToFirst()) {  
                    final int index = cursor.getColumnIndexOrThrow(column);  
                    return cursor.getString(index);  
                }  
            } finally {  
                if (cursor != null)  
                    cursor.close();  
            }  
            return null;  
        }  
          
        /**
         * @param uri The Uri to check.
         * @return Whether the Uri authority is ExternalStorageProvider.
         */  
        public static boolean isExternalStorageDocument(Uri uri) {  
            return "com.android.externalstorage.documents".equals(uri.getAuthority());  
        }  
          
        /**
         * @param uri The Uri to check.
         * @return Whether the Uri authority is DownloadsProvider.
         */  
        public static boolean isDownloadsDocument(Uri uri) {  
            return "com.android.providers.downloads.documents".equals(uri.getAuthority());  
        }  
          
        /**
         * @param uri The Uri to check.
         * @return Whether the Uri authority is MediaProvider.
         */  
        public static boolean isMediaDocument(Uri uri) {  
            return "com.android.providers.media.documents".equals(uri.getAuthority());  
        }  
          
        /**
         * @param uri The Uri to check.
         * @return Whether the Uri authority is Google Photos.
         */  
        public static boolean isGooglePhotosUri(Uri uri) {  
            return "com.google.android.apps.photos.content".equals(uri.getAuthority());  
        }  
    }

时间: 2024-08-11 01:23:37

关于Android4.4的图片路径获取,如果回来的Uri的格式有两种的相关文章

获取网页URL地址及参数等的两种方法(js和C#)

转:获取网页URL地址及参数等的两种方法(js和C#) 一 js 先看一个示例 用javascript获取url网址信息 <script type="text/javascript"> document.write("location.host="+location.host+"<br>"); document.write("location.hostname="+location.hostname+&

【iOS开发-3】sandbox沙盒介绍以及如何取得沙盒里面各文件的路径,图片导入方式和图片路径获取

其实每个APP的程序都是单独的一个文件夹,每个APP之间相互之间独立,所以它们的文件夹也是独立的,这个文件夹就是沙盒.沙盒好处,安全.坏处,程序之间彼此共享数据较为困难. (1)如何找到应用程序的沙盒源文件? 一般沙盒源文件都存放在~/Library/Application Support/iPhone Simulator/版本号/Applications/的文件夹中,但是这个Library(中文名叫资源库)文件夹是隐藏的.所以需要方法才能点击进去:在Finder中按CMD+SHIFT+G,然后

批量改变文件夹和子文件夹中图片格式的两种方法

生活中,我们通常会遇到这种问题:一个文件夹内部有多个子文件夹,每个文件夹内部有很多图片,我们想改变这些图片的格式. 例如.png格式图片支持背景透明,但我们想把它变成背景不透明的.jpg图片.又比如.bmp格式是非压缩图片,我们想把它转换为无损压缩的.png图片以节省存储空间. 如果图片数量很少,很明显我们可以通过Windows自带的画图工具或者其他看图软件通过“另存为”一张张地转换图片格式.但是如果图片很多,怎么办? 这个问题其实有两个关键点: ① 要求批量处理. ② 文件夹中还有子文件夹.

Simics虚拟机Solaris 8操作系统获取host 系统win7上的文件的两种方式

1 介绍 本文基于的环境设置如下: ? 宿主操作系统:Windows 7 Ultimate ? 寄生操作系统:Solaris 8 SPARC (SunOS 5.8) ? 虚拟环境:Simics 3.0.4 本文假定已在Simics 上安装好Solaris 8 SPARC 操作系统. 动机:一个Unix下可以运行的二进制文件GraphGen,在单独的一台装有Ubuntu的电脑上不能运行,因为该电脑的硬件架构是基于X86的,而GraphGen是SPARC架构下才能运行的程序:在我的笔记本Win7系统

iOS 获取Interface Builder上的子控制器的两种方式

原创Blog,转载请注明出处 blog.csdn.net/hello_hwc 准备工作 Storyboard上为一个ViewController拖拽两个子控制器,并且设置两个segue的identifier分别为childvc1,childvc2 效果 方式一,根据segue的identifier来判断获得 #import "ViewController.h" #import "ChildViewController1.h" #import "ChildV

js文件、图片上传(原生方法和jquery的ajax两种都有)

<!DOCTYPE html><html> <head> <title>test</title> </head> <body> <input type="file" id="fileUpload"> <button id="submit">点击上传文件</button> <script src="javascri

在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好

花了一下午时间,终于学会了在数据库中存储图片,以及在界面中显示图片了. 存储图片有两种方法: 一种是:直接把图片转换成二进制文件存储在数据库中. 一种是:存储图片的路径到数据库,用的时候直接调用路径给image等图像控件即可. 两种方法,有利有弊,第一种方法适合存储量少且重要的图片信息.第二种方法适合存储量大但不是太重要的图片. 我使用的是第二种方法,简单,实用. 下面就是我具体的实现过程: 首先我新建了两个网页文件,insert_photo.aspx / select_photo.apsx 第

asp.net 客户端上传文件全路径获取方法

asp.net  获取客户端上传文件全路径方法: eg:F:\test\1.doc 基于浏览器安全问题,浏览器将屏蔽获取客户端文件全路径的方法,只能获取到文件的文件名,如果需要获取全路径则需要另想其他方法 如下提供两种方法: 方法1:工具 -> Internet选项 -> 安全 -> 自定义级别 -> 找到“其他”中的“将本地文件上载至服务器时包含本地目录路径”,选中“启用”即可. 方法2: function getPath() { //获取file 控件对象 var obj =

CKEditor实现图片上传,并且回调图片路径

CKEditor编辑器的工具栏中初始的时候应该是这样子的,没有图片上传按钮 并且预览中有一堆火星文,可以修改相应配置删除它. 第一种方法:打开ckeditor/plugins/image/dialogs/image.js文件,搜索"b.config.image_previewText",(b.config.image_previewText||'')单引号中的内容全删了,注意别删多了.(由于ckeditor的很多js文件都是压缩过的,格式很难看,很容易删错,所以不推荐此种方法) 第二种