https://www.jianshu.com/p/02fa61d8dbf5

工作中遇到的问题。拍照获取图片后是得到的路径是

file:///storage/emulated/0/Android/data/com.zehin.mingchuliangzao3/cache/PostPicture/20160905182015.jpg

但是我想要的路径是:

content://media/external/images/media/212304

这种 Uri类型的

查阅资料找到如下方法

转Uri

/**     * Gets the content:// URI from the given corresponding path to a file     *      * @param context     * @param imageFile     * @return content Uri     */    public static Uri getImageContentUri(Context context, java.io.File imageFile) {        String filePath = imageFile.getAbsolutePath();        Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,                new String[] { MediaStore.Images.Media._ID }, MediaStore.Images.Media.DATA + "=? ",                new String[] { filePath }, null);        if (cursor != null && cursor.moveToFirst()) {            int id = cursor.getInt(cursor.getColumnIndex(MediaStore.MediaColumns._ID));            Uri baseUri = Uri.parse("content://media/external/images/media");            return Uri.withAppendedPath(baseUri, "" + id);        } else {            if (imageFile.exists()) {                ContentValues values = new ContentValues();                values.put(MediaStore.Images.Media.DATA, filePath);                return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);            } else {                return null;            }        }    }

Uri转绝对路径

/**      * Gets the corresponding path to a file from the given content:// URI      * @param selectedVideoUri The content:// URI to find the file path from      * @param contentResolver The content resolver to use to perform the query.      * @return the file path as a string      */      public static String getFilePathFromContentUri(Uri selectedVideoUri,              ContentResolver contentResolver) {          String filePath;          String[] filePathColumn = {MediaColumns.DATA};            Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);  //      也可用下面的方法拿到cursor  //      Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);                    cursor.moveToFirst();            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);          filePath = cursor.getString(columnIndex);          cursor.close();          return filePath;      }  

绝对路径转Uri的那个方法 目前是图片文件的转换 转其他文件 只要把content后面的目录换成对应文件的归属目录就行了。。



作者:青檬可乐_
链接:https://www.jianshu.com/p/02fa61d8dbf5
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

更多相关文章

  1. 一款常用的 Squid 日志分析工具
  2. GitHub 标星 8K+!一款开源替代 ls 的工具你值得拥有!
  3. RHEL 6 下 DHCP+TFTP+FTP+PXE+Kickstart 实现无人值守安装
  4. Linux 环境下实战 Rsync 备份工具及配置 rsync+inotify 实时同步
  5. Android(安卓)之AnimationDrawable
  6. android初涉JNI
  7. android命令gradle打包apk
  8. -Gradle使用手册(一):为什么要用Gradle?
  9. Android网络文件下载模块整理

随机推荐

  1. Android之Widget (微件、小组件)
  2. 细读《深入理解 Android(安卓)内核设计思
  3. Android(安卓)Camera的预览回调接口Previ
  4. Button的高宽无故变大了!
  5. 转Android(安卓)安全攻防(三): SEAndroid(安
  6. Android中Handler消息处理机制原理
  7. EventBus使用详解(二)——EventBus使用进
  8. Android(安卓)轮播图---ViewFlipper
  9. android开发之路04(初级android工程师必会
  10. PopupWindow(一)