2017-04-27 69 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