fastest way to load image thumbnails into grid - java

I'm trying to show a gridview with all the users images in it.
but loading the images from the android MediaStore.
I have to do a request and then loop over the images from the result to get every single thumbnail, this results in a huge lag!
how would I compress these calls into a single request or how would I save more time on this?
String[] projection = new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.TITLE,
MediaStore.Images.Media.DATA,
MediaStore.Images.Media.MIME_TYPE,
MediaStore.Images.Media.SIZE,
MediaStore.Images.Media.DATE_TAKEN,
MediaStore.Images.Media.DATE_ADDED,
MediaStore.Images.Media.DATE_MODIFIED,
MediaStore.Images.Media.ORIENTATION
};
String sortOrder = MediaStore.Images.Media.DATE_ADDED + " DESC, " +
MediaStore.Images.Media.DATE_MODIFIED + " DESC";
ContentResolver cr = ctx.getContentResolver();
final Cursor cursorImages = cr.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
sortOrder);
ArrayList<ImageDataHolder> images = new ArrayList<ImageDataHolder>(cursorImages.getCount());
if (cursorImages.moveToFirst()) {
final int dataColumn = cursorImages.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
final int idColumn = cursorImages.getColumnIndexOrThrow(MediaStore.Images.Media._ID);
do {
final Long imageID = cursorImages.getLong(idColumn);
final String imagePath = cursorImages.getString(dataColumn);
final int imageOrientation = cursorImages.getInt(dataColumn);
// create new cursor to get thumbnail path
final Cursor cursorThumb = MediaStore.Images.Thumbnails.queryMiniThumbnail(ctx.getContentResolver(), imageID,
MediaStore.Images.Thumbnails.MINI_KIND, null);
String thumbPath = "";
if (cursorThumb != null) {
if (cursorThumb.getCount() > 0) {
cursorThumb.moveToFirst();
thumbPath = cursorThumb.getString(cursorThumb.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA));
}
cursorThumb.close(); // cleanup crew, isle 7
}
// create new ImageDataHolder and add to images ArrayList
// only if we have a thumbnail to show
if (thumbPath != "")
images.add(new ImageDataHolder(imagePath, thumbPath, imageID, imageOrientation));
} while (cursorImages.moveToNext());
}
cursorImages.close();
return images;

Related

How to get MediaMetadataCompat from an specific single File

I'm creating an application where you can play all .wav files using the Media App Architecture.
So while user is listening a song, internally app is creating the reverse file of the song and save it in an specific path. It's working fine.
("/storage/emulated/0/VoiceRecorder/reverse-finish.wav").
But when I want to get MediaMetadataCompat from the specific URI, it return null.
This is the code:
public static MediaMetadataCompat getMediaMetadataFromFilePath(Context context,
Uri path) {
String[] projection = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA
};
Cursor audioCursor = context.getContentResolver().query(
MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Audio.Media._ID + " = ?",
new String[]{path.getPath()},
"");
if (audioCursor == null) {
return null;
}
audioCursor.moveToFirst();
int mediaId = audioCursor.getColumnIndexOrThrow(
MediaStore.Audio.Media._ID);
int name = audioCursor.getColumnIndexOrThrow(
MediaStore.Audio.Media.DISPLAY_NAME);
int duration = audioCursor.getColumnIndexOrThrow(
MediaStore.Audio.Media.DURATION);
int title = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
int data = audioCursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
String id = audioCursor.getString(mediaId);
MediaMetadataCompat media = createMediaMetadataCompat(
id, audioCursor.getString(title),
null, null, null,
audioCursor.getLong(duration), TimeUnit.SECONDS,
audioCursor.getString(name), 0, null,
audioCursor.getString(data));
return media;
}
Can you help what's wrong here?

how to get all pdf file available in my phone storage in android 11 scope storage without using manage external storage permission

I am trying to this code. Can someone tell me how can I get only .pdf files in my phone storage?
protected ArrayList<String> getPdfList() {
ArrayList<String> pdfList = new ArrayList<>();
Uri collection;
final String[] projection = new String[]{
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.MIME_TYPE,
};
final String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
final String[] selectionArgs = new String[]{mimeType};
if (SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
} else {
collection = MediaStore.Files.getContentUri("external");
}
try (Cursor cursor = getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder)) {
assert cursor != null;
if (cursor.moveToFirst()) {
int columnData = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int columnName = cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME);
do {
if (new File(cursor.getString(columnData)).exists()) {
pdfList.add((cursor.getString(columnData)));
Log.d(TAG, "getPdf: " + cursor.getString(columnData));
//you can get your pdf files
}
} while (cursor.moveToNext());
}
}
return pdfList;
}
above code work fine upto android 10 with requestLegacyExternalStorage = true but in android 11 cursor.moveToFirst() geting false
Add these two line to your manifest:
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />

Fetching pdf file from MediaStore shows the list of data that doesnt even exist in internal storage and sd card

I want to know how can i use mediastore to check whether the file that i am fetching from my storage is deleted or present in my storage.The problem that i am facing is my pdf app is showing the deleted pdf which were deleted maybe 2-4 months earlier.I am sharing my code please let me know how to solve this issue.
Here is my code:
protected ArrayList<String> getPdfList() {
ArrayList<String> pdfList = new ArrayList<>();
Uri collection;
final String[] projection = new String[]{
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.MIME_TYPE,
};
final String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";
final String selection = MediaStore.Files.FileColumns.MIME_TYPE + " = ?";
final String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
final String[] selectionArgs = new String[]{mimeType};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
collection = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
}else{
collection = MediaStore.Files.getContentUri("external");
}
try (Cursor cursor = getContentResolver().query(collection, projection, selection, selectionArgs, sortOrder)) {
assert cursor != null;
if (cursor.moveToFirst()) {
int columnData = cursor.getColumnIndex(MediaStore.Files.FileColumns.DATA);
int columnName = cursor.getColumnIndex(MediaStore.Files.FileColumns.DISPLAY_NAME);
do {
pdfList.add((cursor.getString(columnData)));
Log.d(TAG, "getPdf: " + cursor.getString(columnData));
//you can get your pdf files
} while (cursor.moveToNext());
}
}
return pdfList;
}

Retrieve images from users phone

I have created a functional image gallery that takes photos from a user's phone and displays it on a recyclerview. Everything works, however, it seems as though the method I have used to retrieve this data is limited to API level 29. The app is meant to work on devices API 21 and up. How can I adjust this code to accommodate this?
RetrieveImgaes.java
public class SelectImagesGallery {
public static ArrayList < String > listOfImages(Context context) {
Uri uri;
Cursor cursor;
int column_index_data, column_index_folder_name;
ArrayList < String > listOfAllImages = new ArrayList < > ();
String absolutePathOfImage;
uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
String[] projection = {
MediaStore.MediaColumns.DATA,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME
};
String orderBy = MediaStore.Video.Media.DATE_TAKEN;
cursor = context.getContentResolver().query(uri, projection, null, null, orderBy + " DESC");
column_index_data = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
// Get folder name
// column_index_folder_name = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
absolutePathOfImage = cursor.getString(column_index_data);
listOfAllImages.add(absolutePathOfImage);
}
return listOfAllImages;
}

How to get all video from specific folder with path,duration and thumbnail using MediaStore?

I want to get all videos from a specific location(Folder) using MediaStore with
1. Full Video Path
2. Video Thumbnail
3. Video Duration
if anyone has an idea about how can I get all the above things then give your suggestion & help
Thanks in advance.
Uri mainUri;
Cursor cursor1 = getContentResolver().query(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
new String[]{MediaStore.Video.Media._ID},
MediaStore.Video.Media.DATA + "=? ",
new String[]{pathMain}, null);
if (cursor1 != null && cursor1.moveToFirst()) {
int id = cursor1.getInt(cursor1.getColumnIndex(MediaStore.MediaColumns._ID));
cursor1.close();
mainUri = Uri.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, "" + id);
} else {
ContentValues values = new ContentValues();
values.put(MediaStore.Video.Media.DATA, pathMain);
mainUri = getContentResolver().insert(
MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
}
final String[] projection = new String[]{MediaStore.Video.Media._ID, MediaStore.Video.Media.DISPLAY_NAME, MediaStore.Video.Media.DATA,
MediaStore.Video.Media.DURATION};
String selection = MediaStore.Video.Media.DATA + "=?";
String selectionArgs[] = {pathMain};
CursorLoader loader = new CursorLoader(getActivity(), mainUri, projection, selection, selectionArgs, null);
Cursor cursor = loader.loadInBackground();
if (cursor != null && cursor.moveToFirst()) {
long id = cursor.getLong(cursor.getColumnIndex(projection[0]));
String name = cursor.getString(cursor.getColumnIndex(projection[1]));
String path = cursor.getString(cursor.getColumnIndex(projection[2]));
long duration = cursor.getLong(cursor.getColumnIndex(projection[3]));
cursor.close();
return new ImageObject(id, name, path, false, MEDIA_TYPE_VIDEO, duration);
}

Categories