0
点赞
收藏
分享

微信扫一扫

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


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

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

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

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

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. 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. new String[] { ImageColumns.DATA }, null, null, null );  
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呢?



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. "path1 is " + path);  
6. if (path != null) {  
7.         path = Uri.decode(path);  
8. "path2 is " + path);  
9. this.getContentResolver();  
10. new StringBuffer();  
11. "(")  
12.                 .append(Images.ImageColumns.DATA)  
13. "=")  
14. "'" + path + "'")  
15. ")");  
16.         Cursor cur = cr.query(  
17.                 Images.Media.EXTERNAL_CONTENT_URI,  
18. new String[] { Images.ImageColumns._ID },  
19. null, null);  
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. "content://media/external/images/media/"  
32.                             + index);  
33. "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获得其在文件系统中的路径呢?

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

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. 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. new String[] { ImageColumns.DATA }, null, null, null );  
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呢?


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. "path1 is " + path);  
6. if (path != null) {  
7.         path = Uri.decode(path);  
8. "path2 is " + path);  
9. this.getContentResolver();  
10. new StringBuffer();  
11. "(")  
12.                 .append(Images.ImageColumns.DATA)  
13. "=")  
14. "'" + path + "'")  
15. ")");  
16.         Cursor cur = cr.query(  
17.                 Images.Media.EXTERNAL_CONTENT_URI,  
18. new String[] { Images.ImageColumns._ID },  
19. null, null);  
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. "content://media/external/images/media/"  
32.                             + index);  
33. "uri_temp is " + uri_temp);  
34. if (uri_temp != null) {  
35.                 uri = uri_temp;  
36.             }  
37.         }  
38.     }  
39. }

举报

相关推荐

0 条评论