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

來源:互聯網
上載者:User

最近做項目要通過圖片的絕對路徑找到圖片的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,不知這兩種有什麼區別,望大神指教一二!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.