Android get images from sdcard

2020-06-25 05:09发布

问题:

I am using a code which is listing all the images from my device...and I'm trying to figure it out how to get images only from a specific folder, not all the images. Here is the code I'm using :

ArrayList<Bitmap> images = new ArrayList<Bitmap>();
String[] projection = {MediaStore.Images.Thumbnails.DATA};
Uri uri = Uri.parse("content://media/external/images/media");
cursor = managedQuery( uri, projection, null, null, null);
//cursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
Log.i("MediaStore.Images.Media.EXTERNAL_CONTENT_URI", "MediaStore.Images.Media.EXTERNAL_CONTENT_URI: " + MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
if(cursor.getCount()==0){
     Log.i("No Cards","No Cards");
     cursor.close();
 } else if(cursor.getCount()>0){        
 for(cursor.moveToFirst(); cursor.moveToNext(); cursor.isAfterLast()){
     int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
     String imagePath = cursor.getString(columnIndex);
     Log.i("imagePath", "imagePath: " + imagePath);
     Bitmap b = BitmapFactory.decodeFile("/Stampii" + imagePath, null);
     images.add(b);
  }
  }

The thing that I want to do is to get images from imagePath: /mnt/sdcard/Stampii/MediaCategory-251.jpg Stampii folder, but I can't understand how to enter the right path to that folder. I've already tried with :

Uri uri = Uri.parse("content://media/external/images/media/mnt/sdcard/Stampii");

Any solutions?

回答1:

use

File file = new File(Environment.getExternalStoragePath()+"/Stampii/");

file imageList[] = file.listFiles();

 for(int i=0;i<imageList.length;i++)
 {
   Log.e("Image: "+i+": path", imageList[i].getAbsolutePath());

   Bitmap b = BitmapFactory.decodeFile(imageList[i].getAbsolutePath());

   images.add(b);

 }