国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

android圖片文件的路徑地址與Uri的相互轉換方法

2019-10-23 18:29:55
字體:
來源:轉載
供稿:網友

一個android文件的Uri地址一般如下:

content://media/external/images/media/62026

這是一張圖片的Uri,那么我們如何根據這個Uri獲得其在文件系統中的路徑呢?

其實很簡單,直接上代碼:

public static String getRealFilePath( final Context context, final Uri uri ) {  if ( null == uri ) return null;  final String scheme = uri.getScheme();  String data = null;  if ( scheme == null )    data = uri.getPath();  else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {    data = uri.getPath();  } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {    Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, null, null, null );    if ( null != cursor ) {      if ( cursor.moveToFirst() ) {        int index = cursor.getColumnIndex( ImageColumns.DATA );        if ( index > -1 ) {          data = cursor.getString( index );        }      }      cursor.close();    }  }  return data  }

如我們有一個圖片的路徑地址又該如何獲得其Uri呢?

  String type = Utils.ensureNotNull(intent.getType());        Log.d(TAG, "uri is " + uri);        if (uri.getScheme().equals("file") && (type.contains("image/"))) {          String path = uri.getEncodedPath();          Log.d(TAG, "path1 is " + path);          if (path != null) {            path = Uri.decode(path);            Log.d(TAG, "path2 is " + path);            ContentResolver cr = this.getContentResolver();            StringBuffer buff = new StringBuffer();            buff.append("(")                .append(Images.ImageColumns.DATA)                .append("=")                .append("'" + path + "'")                .append(")");            Cursor cur = cr.query(                Images.Media.EXTERNAL_CONTENT_URI,                new String[] { Images.ImageColumns._ID },                buff.toString(), null, null);            int index = 0;            for (cur.moveToFirst(); !cur.isAfterLast(); cur                .moveToNext()) {              index = cur.getColumnIndex(Images.ImageColumns._ID);              // set _id value              index = cur.getInt(index);            }            if (index == 0) {              //do nothing            } else {              Uri uri_temp = Uri                  .parse("content://media/external/images/media/"                      + index);              Log.d(TAG, "uri_temp is " + uri_temp);              if (uri_temp != null) {                uri = uri_temp;              }            }          }        }

以上這篇android圖片文件的路徑地址與Uri的相互轉換方法就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 广元市| 万源市| 申扎县| 石首市| 万荣县| 大宁县| 普宁市| 海宁市| 清远市| 平江县| 宜宾市| 镇平县| 宁南县| 镇平县| 阳信县| 泽普县| 兰州市| 宜州市| 额尔古纳市| 浦北县| 沈阳市| 鄱阳县| 吴川市| 东山县| 崇礼县| 南部县| 石首市| 济宁市| 维西| 济阳县| 许昌县| 台江县| 南漳县| 洛川县| 东乌珠穆沁旗| 江口县| 饶阳县| 崇信县| 新绛县| 乳源| 丹巴县|