Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android系統通過圖片絕對路徑獲取URI的三種方法

android系統通過圖片絕對路徑獲取URI的三種方法

編輯:關於Android編程

最近做項目要通過圖片的絕對路徑找到圖片的URI,然後刪除圖片,小小總結一下獲取URI的方法,親自試驗在

android 4.1.3的系統上都是可用的。

 

1.將所有的圖片路徑取出,遍歷比較找到需要的路徑,取出URI,效率較低

其中 MediaStore.MediaColumns.DATA 字段存的就是圖片的絕對路徑,

最後mImageUri得到的就是圖片的URI


 1 Uri mUri = Uri.parse("content://media/external/images/media");
 2 Uri mImageUri = null;
 3 Cursor cursor = managedQuery(
 4                                 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
 5                                 MediaStore.Images.Media.DEFAULT_SORT_ORDER);
 6 cursor.moveToFirst();
 7
 8 while (!cursor.isAfterLast()) {
 9                         String data = cursor.getString(cursor
10                                         .getColumnIndex(MediaStore.MediaColumns.DATA));
11                         if (picPath.equals(data)) {
12                                 int ringtoneID = cursor.getInt(cursor
13                                                 .getColumnIndex(MediaStore.MediaColumns._ID));
14                                 mImageUri = Uri.withAppendedPath(mUri, "" + ringtoneID);
15                                 break;
16                         }
17                         cursor.moveToNext();
18 }

2.直接從媒體數據庫根據字段取出相應的記錄,效率較高


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

3.直接根據路徑通過 ContentProvider 的 delete() 方法刪除圖片,兩行代碼搞定,效率最高

1 String params[] = new String[]{filepath};
2 ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + " LIKE ?", params);

PS:通過URI刪除圖片可以刪除媒體庫的信息和縮略圖一並刪除,不留痕跡,如果直接通過路徑刪除文件會刪除不干淨

前兩種方法獲取的都是content開頭的URI,還有一種file開頭的URI,不知這兩種有什麼區別,望大神指教一二!

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved