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. }  
發佈了5 篇原創文章 · 獲贊 12 · 訪問量 11萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章