Display all images from the device in the application
I am using below code to get all images from Camera folder inside DCIM and display in my application. But I want to display all images on the device in my application, no matter where they are stored on the device. How can i do this?
String ExternalStorageDirectoryPath = Environment
.getExternalStorageDirectory()
.getAbsolutePath();
String targetPath = ExternalStorageDirectoryPath + "/DCIM/Camera";
images=new ArrayList<String>();
File targetDirector = new File(targetPath);
File[] files = targetDirector.listFiles();
for (File file : files) {
images.add(file.getAbsolutePath());
}
gallerylist=new CameraGalleryAdapter(getActivity().getApplicationContext(),R.layout.giphy_grid,images);
gridview.setAdapter(gallerylist);
+3
source to share
1 answer
First of all, you should check if the SD card is available:
Boolean isSDPresent = android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
If SD card is present use this:
final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
//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);
Log.i("PATH", arrPath[i]);
}
// The cursor should be freed up after use with close()
cursor.close();
The above code will list all images from the SD card. To retrieve images from internal memory, simply replace
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
from
MediaStore.Images.Media.INTERNAL_CONTENT_URI
Using the same piece of code.
+8
source to share