Retrieve images from users phone - java

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

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

fastest way to load image thumbnails into grid

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;

List all of one file type on Android device?

For example, I want to retrieve a list of all .mp3 files on the internal AND external storage. I would also like the path (String) of each .mp3 file for reference (store them in a List?) How could I do this? Is this a relatively expensive operation? If so, should it be put on a thread and while that is running, present the user with a "Loading" ProgressDialog?
Please find the function below . It will get all the mp3 files stored in external and Internal memory .
public void getAllSongs() {
Uri allsongsuri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.IS_MUSIC + " != 0";
cursor = managedQuery(allsongsuri, STAR, selection, null, null);
if (cursor != null) {
if (cursor.moveToFirst()) {
do {
song_name = cursor
.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DISPLAY_NAME));
int song_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media._ID));
String fullpath = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.DATA));
fullsongpath.add(fullpath);
album_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM));
int album_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
artist_name = cursor.getString(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST));
int artist_id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID));
} while (cursor.moveToNext());
}
cursor.close();
db.closeDatabase();
}
}

Categories