Android get images from sdcard

允我心安 提交于 2019-12-21 05:35:16

问题


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);

 }


来源:https://stackoverflow.com/questions/7821478/android-get-images-from-sdcard

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!