content Uri to path

/** * 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;}

path to content 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;            }        }    }


更多相关文章

  1. android handler和message的常用方法
  2. Android通过广播接收者调用服务内方法
  3. android 一步一步创建jni示例
  4. Telephony单元测试分析
  5. Android(安卓)开启关闭软键盘
  6. Android数据库升级、降级、创建(onCreate() onUpgrade() onDowng
  7. RecyclerView的canScrollVertically方法踩坑
  8. Property Anim详解
  9. AndroidGUI13:ViewTreeObserver的常用技巧

随机推荐

  1. Android 自动编译、打包生成apk文件 3 -
  2. ViewStub的使用
  3. 几个Android控件属性笔记
  4. 译:Android(安卓)N不再支持通过Intent传递
  5. Finished with error: ProcessException:
  6. Android(安卓)NDK 错误解决方法(持续更新
  7. 手把手教你搞懂 Android 反编译
  8. Android Studio中如何隐藏顶部状态栏和标
  9. 《Android经验分享》周刊第1期
  10. 【源码分享下载】每日更新之Android项目