android 路径地址与Uri的相互转换 uri转string

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

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

这是一张图片的Uri,那么我们如何根据这个Uri获得其在文件系统中的路径呢?

其实很简单,直接上代码:

[javascript]  view plain  copy
  1. /** 
  2.  * Try to return the absolute file path from the given Uri 
  3.  * 
  4.  * @param context 
  5.  * @param uri 
  6.  * @return the file path or null 
  7.  */  
  8. public static String getRealFilePath( final Context context, final Uri uri ) {  
  9.     if ( null == uri ) return null;  
  10.     final String scheme = uri.getScheme();  
  11.     String data = null;  
  12.     if ( scheme == null )  
  13.         data = uri.getPath();  
  14.     else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {  
  15.         data = uri.getPath();  
  16.     } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {  
  17.         Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, nullnullnull );  
  18.         if ( null != cursor ) {  
  19.             if ( cursor.moveToFirst() ) {  
  20.                 int index = cursor.getColumnIndex( ImageColumns.DATA );  
  21.                 if ( index > -1 ) {  
  22.                     data = cursor.getString( index );  
  23.                 }  
  24.             }  
  25.             cursor.close();  
  26.         }  
  27.     }  
  28.     return data;  
  29. }  

那么假如我们有一个图片的路径地址又该如何获得其Uri呢?

[javascript]  view plain  copy
  1. String type = Utils.ensureNotNull(intent.getType());  
  2. Log.d(TAG, "uri is " + uri);  
  3. if (uri.getScheme().equals("file") && (type.contains("image/"))) {  
  4.     String path = uri.getEncodedPath();  
  5.     Log.d(TAG, "path1 is " + path);  
  6.     if (path != null) {  
  7.         path = Uri.decode(path);  
  8.         Log.d(TAG, "path2 is " + path);  
  9.         ContentResolver cr = this.getContentResolver();  
  10.         StringBuffer buff = new StringBuffer();  
  11.         buff.append("(")  
  12.                 .append(Images.ImageColumns.DATA)  
  13.                 .append("=")  
  14.                 .append("'" + path + "'")  
  15.                 .append(")");  
  16.         Cursor cur = cr.query(  
  17.                 Images.Media.EXTERNAL_CONTENT_URI,  
  18.                 new String[] { Images.ImageColumns._ID },  
  19.                 buff.toString(), nullnull);  
  20.         int index = 0;  
  21.         for (cur.moveToFirst(); !cur.isAfterLast(); cur  
  22.                 .moveToNext()) {  
  23.             index = cur.getColumnIndex(Images.ImageColumns._ID);  
  24.             // set _id value  
  25.             index = cur.getInt(index);  
  26.         }  
  27.         if (index == 0) {  
  28.             //do nothing  
  29.         } else {  
  30.             Uri uri_temp = Uri  
  31.                     .parse("content://media/external/images/media/"  
  32.                             + index);  
  33.             Log.d(TAG, "uri_temp is " + uri_temp);  
  34.             if (uri_temp != null) {  
  35.                 uri = uri_temp;  
  36.             }  
  37.         }  
  38.     }  
  39. }  

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

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

这是一张图片的Uri,那么我们如何根据这个Uri获得其在文件系统中的路径呢?

其实很简单,直接上代码:

[javascript]  view plain  copy
  1. /** 
  2.  * Try to return the absolute file path from the given Uri 
  3.  * 
  4.  * @param context 
  5.  * @param uri 
  6.  * @return the file path or null 
  7.  */  
  8. public static String getRealFilePath( final Context context, final Uri uri ) {  
  9.     if ( null == uri ) return null;  
  10.     final String scheme = uri.getScheme();  
  11.     String data = null;  
  12.     if ( scheme == null )  
  13.         data = uri.getPath();  
  14.     else if ( ContentResolver.SCHEME_FILE.equals( scheme ) ) {  
  15.         data = uri.getPath();  
  16.     } else if ( ContentResolver.SCHEME_CONTENT.equals( scheme ) ) {  
  17.         Cursor cursor = context.getContentResolver().query( uri, new String[] { ImageColumns.DATA }, nullnullnull );  
  18.         if ( null != cursor ) {  
  19.             if ( cursor.moveToFirst() ) {  
  20.                 int index = cursor.getColumnIndex( ImageColumns.DATA );  
  21.                 if ( index > -1 ) {  
  22.                     data = cursor.getString( index );  
  23.                 }  
  24.             }  
  25.             cursor.close();  
  26.         }  
  27.     }  
  28.     return data;  
  29. }  

那么假如我们有一个图片的路径地址又该如何获得其Uri呢?

[javascript]  view plain  copy
  1. String type = Utils.ensureNotNull(intent.getType());  
  2. Log.d(TAG, "uri is " + uri);  
  3. if (uri.getScheme().equals("file") && (type.contains("image/"))) {  
  4.     String path = uri.getEncodedPath();  
  5.     Log.d(TAG, "path1 is " + path);  
  6.     if (path != null) {  
  7.         path = Uri.decode(path);  
  8.         Log.d(TAG, "path2 is " + path);  
  9.         ContentResolver cr = this.getContentResolver();  
  10.         StringBuffer buff = new StringBuffer();  
  11.         buff.append("(")  
  12.                 .append(Images.ImageColumns.DATA)  
  13.                 .append("=")  
  14.                 .append("'" + path + "'")  
  15.                 .append(")");  
  16.         Cursor cur = cr.query(  
  17.                 Images.Media.EXTERNAL_CONTENT_URI,  
  18.                 new String[] { Images.ImageColumns._ID },  
  19.                 buff.toString(), nullnull);  
  20.         int index = 0;  
  21.         for (cur.moveToFirst(); !cur.isAfterLast(); cur  
  22.                 .moveToNext()) {  
  23.             index = cur.getColumnIndex(Images.ImageColumns._ID);  
  24.             // set _id value  
  25.             index = cur.getInt(index);  
  26.         }  
  27.         if (index == 0) {  
  28.             //do nothing  
  29.         } else {  
  30.             Uri uri_temp = Uri  
  31.                     .parse("content://media/external/images/media/"  
  32.                             + index);  
  33.             Log.d(TAG, "uri_temp is " + uri_temp);  
  34.             if (uri_temp != null) {  
  35.                 uri = uri_temp;  
  36.             }  
  37.         }  
  38.     }  
  39. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安果移不动

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值