zoukankan      html  css  js  c++  java
  • 根据Uri获取文件的绝对路径

    简易版处理(实际并没发现有什么问题)

    public static String getRealPathFromURI(Context context, Uri contentURI) {
    String result;
    Cursor cursor = context.getContentResolver().query(contentURI,
    new String[]{MediaStore.Images.ImageColumns.DATA},//
    null, null, null);
    if (cursor == null) result = contentURI.getPath();
    else {
    cursor.moveToFirst();
    int index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
    result = cursor.getString(index);
    cursor.close();
    }
    return result;
    }

    复杂版处理

    /**
    * 根据图片的Uri获取图片的绝对路径(适配多种API)
    *
    * @return 如果Uri对应的图片存在, 那么返回该图片的绝对路径, 否则返回null
    */
    public static String getRealPathFromUri(Context context, Uri uri) {
    int sdkVersion = Build.VERSION.SDK_INT;
    if (sdkVersion < 11) return getRealPathFromUri_BelowApi11(context, uri);
    if (sdkVersion < 19) return getRealPathFromUri_Api11To18(context, uri);
    else return getRealPathFromUri_AboveApi19(context, uri);
    }

    /**
    * 适配api19以上,根据uri获取图片的绝对路径
    */
    @TargetApi(Build.VERSION_CODES.KITKAT)
    private static String getRealPathFromUri_AboveApi19(Context context, Uri uri) {
    String filePath = null;
    String wholeID = DocumentsContract.getDocumentId(uri);

    // 使用':'分割
    String id = wholeID.split(":")[1];

    String[] projection = {MediaStore.Images.Media.DATA};
    String selection = MediaStore.Images.Media._ID + "=?";
    String[] selectionArgs = {id};

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,//
    projection, selection, selectionArgs, null);
    int columnIndex = cursor.getColumnIndex(projection[0]);
    if (cursor.moveToFirst()) filePath = cursor.getString(columnIndex);
    cursor.close();
    return filePath;
    }

    /**
    * 适配api11-api18,根据uri获取图片的绝对路径
    */
    private static String getRealPathFromUri_Api11To18(Context context, Uri uri) {
    String filePath = null;
    String[] projection = {MediaStore.Images.Media.DATA};
    CursorLoader loader = new CursorLoader(context, uri, projection, null, null, null);
    Cursor cursor = loader.loadInBackground();

    if (cursor != null) {
    cursor.moveToFirst();
    filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
    cursor.close();
    }
    return filePath;
    }

    /**
    * 适配api11以下(不包括api11),根据uri获取图片的绝对路径
    */
    private static String getRealPathFromUri_BelowApi11(Context context, Uri uri) {
    String filePath = null;
    String[] projection = {MediaStore.Images.Media.DATA};
    Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null) {
    cursor.moveToFirst();
    filePath = cursor.getString(cursor.getColumnIndex(projection[0]));
    cursor.close();
    }
    return filePath;
    }





  • 相关阅读:
    多西:一个创立了Twitter与Square的程序员
    越狱黑客Comex结束苹果实习生涯 将专注学校学习生活
    关于从页面中获取用户控件中的其它控件(如DropDownList)事件的方法
    如何更改VS2010的[默认开发语言]默认环境设置
    TRULY Understanding ViewState exactl
    .net平台下xml操作
    正则表达式学习2
    正则表达式学习3
    petshop 出现没有为 SQL 缓存通知启用数据库“MSPetShop4”
    利用ashx以XML返回的形式和ajax实现表格的异步填充
  • 原文地址:https://www.cnblogs.com/baiqiantao/p/6795684.html
Copyright © 2011-2022 走看看