2012-04-09 26 views
3

我有這樣的代碼(在此網站上發現,某處):得到縮略圖查詢圖像的android

public static List<MyImages> getImages(Activity context) { 
    List<MyImages> lst = new ArrayList<MyImages>(); 
    Cursor cursor = getCameraThumbImages(context); 
    if (cursor != null) { 
     int columnIndex = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID); 
     int columnIndexPath = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA); 
     int columnIndexImagePath = cursor 
       .getColumnIndexOrThrow(MediaStore.Images.Thumbnails.IMAGE_ID); 
     int count = cursor.getCount(); 
     for (int i = 0; i < count; i++) { 
      cursor.moveToPosition(i); 

      int imageID = cursor.getInt(columnIndex); 
      String path = cursor.getString(columnIndexPath); 
      Uri imgThmbPath = Uri.withAppendedPath(
        MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" 
          + imageID); 
      String hope = cursor.getString(columnIndexImagePath); 
      MyImages p2p = new MyImages(path, "" + imageID); 
      lst.add(p2p); 
     } 
    } 

    return lst; 
} 

此代碼允許我使用我的手機上的圖像的縮略圖。問題是我沒有看到如何從中獲得原始圖像路徑。

的問題是:給出縮略圖(或方向),我如何獲得原始圖像路徑?

回答

2

在縮略圖你有MediaStore.Images.Thumbnails.IMAGE_ID場,你可以從中獲得相關的圖片ID。比對MediaStore.Images.Media進行查詢並從MediaStore.Images.Media.DATA字段獲取照片的路徑。

編輯

// First request thumbnails what you want 
String[] projection = new String[] {MediaStore.Images.Thumbnails._ID, MediaStore.Images.Thumbnails.IMAGE_ID}; 
Cursor thumbnails = contentResolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); 

// Then walk thru result and obtain imageId from records 
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) { 
    String imageId = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.IMAGE_ID)); 

    // Request image related to this thumbnail 
    String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

    Cursor images = contentResolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, filePathColumn, MediaStore.Images.Media._ID + "=?", new String[] {imageId}, null); 

    if (cursor != null && cursor.moveToFirst()) { 
     // Your file-path will be here 
     String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0])); 
    } 

} 

//Of course you need to restrict queries using selection and selection args params and get only rows that you really need 
+0

感謝您的評論。你能否給我這樣做的代碼(或者一些代碼的鏈接)?我不完全瞭解如何執行查詢。再次感謝。 – 2012-05-28 12:23:47

+0

與往常一樣查詢內容提供商 – 2012-05-28 14:22:24

+0

,謝謝。我會檢查你的代碼,並讓你知道它是否解決了我的問題。 – 2012-05-28 17:16:16