How can I display all images from a specific folder on android gallery like, for example, whatapp does. I`m using MediaScannerConnectionClient
File folder =
You can use android.database.Cursor
public boolean OpenGalleryFromFolder(android.content.Context context, String folderName)
{
String filePath = android.os.Environment.getExternalStorageDirectory().getPath() + "/Pictures/" + folderName + "/";
return OpenGalleryFromPathToFolder(context, filePath);
}
// Finds the first image in the specified folder and uses it to open a the devices native gallery app with all images in that folder.
public boolean OpenGalleryFromPathToFolder(android.content.Context context, String folderPath)
{
java.io.File folder = new java.io.File(folderPath);
java.io.File[] allFiles = folder.listFiles();
if (allFiles != null && allFiles.length > 0)
{
android.net.Uri imageInFolder = getImageContentUri(context, allFiles[0]);
if (imageInFolder != null)
{
android.content.Intent intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setData(imageInFolder);
context.startActivity(intent);
return true;
}
}
return false;
}
// converts the absolute path of a file to a content path
// absolute path example: /storage/emulated/0/Pictures/folderName/Image1.jpg
// content path example: content://media/external/images/media/47560
private android.net.Uri getImageContentUri(android.content.Context context, java.io.File imageFile) {
String filePath = imageFile.getAbsolutePath();
android.database.Cursor cursor = context.getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{android.provider.MediaStore.Images.Media._ID},
android.provider.MediaStore.Images.Media.DATA + "=? ",
new String[]{filePath}, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor.getColumnIndex(android.provider.MediaStore.MediaColumns._ID));
return android.net.Uri.withAppendedPath(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
if (imageFile.exists()) {
android.content.ContentValues values = new android.content.ContentValues();
values.put(android.provider.MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}