2017-04-27 95 views
1

我需要獲取相機(圖庫)拍攝的所有圖像的uri /路徑。我如何修改下面的代碼,只給圖片庫中的圖片。下面的代碼也給我從其他文件夾的圖像。僅從圖庫中獲取圖像

public ArrayList<String> getImages() 
    { 
     ArrayList<String> paths = new ArrayList<String>(); 
     final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; 
     final String orderBy = MediaStore.Images.Media.DATE_ADDED; 
     //Stores all the images from the gallery in Cursor 
     Cursor cursor = getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, 
       null, orderBy); 
     //Total number of images 
     int count = cursor.getCount(); 

     //Create an array to store path to all the images 
     String[] arrPath = new String[count]; 

     for (int i = 0; i < count; i++) { 
      cursor.moveToPosition(i); 
      int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); 
      //Store the path of the image 
      arrPath[i]= cursor.getString(dataColumnIndex); 
      paths.add(arrPath[i]); 

     } 

     return paths; 
    } 
+0

你可能會在這裏找到解決辦法... [鏈接](http://stackoverflow.com/questions/32652762/how-to-get -image-uri-from-gallery) –

回答

1

只需提供選擇參數查詢

public ArrayList<String> getImages() 
    { 
     ArrayList<String> paths = new ArrayList<String>(); 
     final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID }; 
     String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " = ?"; 
     String[] selectionArgs = new String[] { 
      "Camera" 
     }; 
     final String orderBy = MediaStore.Images.Media.DATE_ADDED; 
     //Stores all the images from the gallery in Cursor 
     Cursor cursor = getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, selection, selectionArgs, orderBy); 
     //Total number of images 
     int count = cursor.getCount(); 

     //Create an array to store path to all the images 
     String[] arrPath = new String[count]; 

     for (int i = 0; i < count; i++) { 
      cursor.moveToPosition(i); 
      int dataColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA); 
      //Store the path of the image 
      arrPath[i]= cursor.getString(dataColumnIndex); 
      paths.add(arrPath[i]); 

     } 

     return paths; 
    } 
+0

@ Avi-非常感謝! – amanda45

+0

你是最歡迎的 – Avi