I am currently using this code to access the recent images from /DCIM/ folder -
final Cursor cursor = chatActivity.this.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null,
null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
and then get the images by
if (cursor.moveToFirst()) {
String imageLocation = cursor.getString(1);
File imageFile = new File(imageLocation);
if (imageFile.exists()) {
Bitmap bm = BitmapFactory.decodeFile(imageLocation);
}
}
and then using cursor.moveToNext() to access the next image.
But if I have to access the images in another folder (like /MyFolder/Images/) in a similar fashion, using MediaStore and Cursor then what should be done?
I have looked at Get MediaStore path of a Specific Folder and Displaying images from a specific folder on the SDCard using a gridview, they don't answer my question.
With that code you are not only getting images from /DCIM/ folder but from all folders. If you want to get images from specific folder you can use something like this:
final Cursor cursor = chatActivity.this.getContentResolver()
.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, MediaStore.Images.Media.DATA + " like ?",
"%" + Environment.getExternalStorageDirectory().getAbsolutePath() + "/DCIM/%", MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
Related
I got a MP4 File (MIME_TYPE = audio/x-m4a) which I can't find quering mediastore.
I don't know if it's a video or an audio file, so I try to query File table of mediastore (also checke video and audio table with same result).
The file is stored in Music/MP4TestFiles Folder on internal storage (not on an SD-Card, not stored in internal app storage).
Testing on Android 13.
Uri uri = MediaStore.Files.getContentUri(MediaStore.VOLUME_EXTERNAL);
String audiodir = "%MP4TestFiles%";
String[] projection = new String[] {
MediaStore.Files.FileColumns.MIME_TYPE,
MediaStore.Files.FileColumns.DATA,
MediaStore.Files.FileColumns.DISPLAY_NAME,
MediaStore.Files.FileColumns.DATE_ADDED,
MediaStore.Files.FileColumns._ID};
String selection = MediaStore.Files.FileColumns.RELATIVE_PATH+ " like ? ";
String[] selectionArgs = new String[] {audiodir};
String sortOrder = MediaStore.Files.FileColumns.DATE_ADDED + " ASC";
Cursor cur = getContentResolver().query(uri, projection, selection, selectionArgs,sortOrder);
if (cur == null) {
Log.e(TAG, "mp4...no file found in Directory: "+audiodir);
} else if (!cur.moveToFirst()) {
Log.e(TAG, "mp4 Failed to move cursor to first row (no query results). 1");
} else {
Log.d(TAG, "mp4 files found")
}
Permission is set in Manifest:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage"/>
If I rename the file into m4b, I got a result.
Any suggestions what's going wrong?
Thanks,
GGK
I developed an app which saves photos inside the app folder (/android/data/[app-folder]/files/Report/[directory]) with some data saved into DESCRIPTION field, here is my code to save image:
ContentValues image = new ContentValues();
image.put(Images.Media.TITLE, directory);
image.put(Images.Media.DISPLAY_NAME, FOTOC_PREFIX_NAME + "_" + directory + "_" + data + ".jpg");
image.put(Images.Media.DESCRIPTION, "EXAMPLE DESC");
image.put(Images.Media.DATE_ADDED, System.currentTimeMillis());
image.put(Images.Media.MIME_TYPE, "image/jpg");
image.put(Images.Media.ORIENTATION, 0);
File parent = imagePath.getParentFile();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
}
image.put(Images.ImageColumns.BUCKET_ID, parent.toString().toLowerCase().hashCode());
image.put(Images.ImageColumns.BUCKET_DISPLAY_NAME, parent.getName().toLowerCase());
image.put(Images.Media.SIZE, imagePath.length());
image.put(Images.Media.DATA, imagePath.getAbsolutePath());
Uri result = getContentResolver().insert(MediaStore.Images.Media.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), image);
And then i read data with:
String whereCause = MediaStore.Images.Media.DISPLAY_NAME + " LIKE '%" + UsefullThings.getNameWithoutExtension(photofile.getName()) + "%'";
String[] projection = new String[] {MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DESCRIPTION,MediaStore.Images.Media.TITLE,MediaStore.Images.Media._ID};
Cursor cursor = getContentResolver().query(MediaStore.Images.Media
.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY), projection, whereCause, null, MediaStore.Images.Media._ID);
if (cursor.getCount() == 1) {
cursor.moveToFirst();
}
String descstring = "";
try {
descstring =cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DESCRIPTION));
}catch(Exception e){
....
}
where photofile is the image.
Everything is working fine, but if i reboot my tablet then the same query returns empty cursor.
Can anyone help me?
Thanks
app which saves photos inside the app folder (/android/data/[app-folder]/files/Report/[directory])
You mean /storage/emulated/0/Android/data/[app-folder]/files/Report/[directory] ?
That is impossible to begin with if you use the MediaStore.
That is an app specific directory and the MediaStore will not give you insert() uries for that location.
image.put(MediaStore.Images.ImageColumns.RELATIVE_PATH, parent.getAbsolutePath());
You should put a relative path in .RELATIVE_PATH. Not a full absolute path as you do now. Also we cannot see what exactly you put in there. Please tell value of parent.getAbsolutePath().
Hello guys i am trying to get the absolute path of audio file in android api level 29 i.e, Q but the problem is that DATA is deprecated so how to resolve this problem and also using the absolute path (e.x : storage/sdcard/music.mp3) we can get the embedded picture to that audio but in content uri (content://) that we can get using the _ID Column by appending the id of the file.But if we are not able to to get the absolute path then how to play an audio file using content:// Uri ?
The previous codes that i am using is :
public ArrayList<Song> FolderFilesList(Context context, String FolderPath) {
//folder-exclude-sub-folders
ArrayList<Song> songs = new ArrayList<>();
String[] columns = {
MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.TITLE,
MediaStore.Audio.Media.ARTIST,
MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media.DATA,
MediaStore.Audio.Media.ALBUM,
MediaStore.Audio.Media.ALBUM_ID,
MediaStore.Audio.Media.TRACK,
MediaStore.Audio.Media.ARTIST_ID,
MediaStore.Audio.Media.DISPLAY_NAME,
};
String selection = MediaStore.Audio.Media.DATA + " LIKE ? AND " + MediaStore.Audio.Media.DATA + " NOT LIKE ? ";
String[] selectionArgs = new String[]{
"%" + FolderPath + "%",
"%" + FolderPath + "/%/%"
};
Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
columns(), selection, selectionArgs, null);
if (cursor.moveToFirst()) {
do {
Song song = new Song(
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media._ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.TITLE)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST_ID)),
cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)),
cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Media.TRACK)),
cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.DURATION))
);
//File f = new File(cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Media.DATA)));
// if (f.isFile() && f.exists()) {
songs.add(song);
//}
} while (cursor.moveToNext());
}
if (cursor != null) {
cursor.close();
}
return songs;
}
You won't be able to get the absolute path now but you can get content uri and using that content uri means (content://) and use it to write file in your cache directory of your package folder using the below code :
String filePath = new File(resolver.openFileDescriptor(contentUri, "r");
And then use InputStream and OutputStream to store it in cache directory and use this filePath to as a File.
Use this line in application in the Manifest file
android:requestLegacyExternalStorage="true"
I want to load the cover art of a Audio/Music file which is present in Internal Storage of the device.
I Only have the path to the file.
This is what I have tried till now-
I have referred this Issue which show the solution.
But I don't know how to exactly create an new Module, as from what information I could gather online is we have to register the Module first then use them
Any one who could guide me on how to register the Module and then load the cover art would be really helpful.
Also I have tried loading the cover art the normal way like but it does not load the cover art.
GlideApp
.with(myFragment)
.load(url)
.centerCrop()
.placeholder(R.drawable.loading_spinner)
.into(myImageView);
If this is the path you are having :
/storage/emulated/0/Download/mysong.mp3
Then you can use following function :
public void getAudioAlbumImageContentUri(Context context, String filePath) {
Uri audioUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
String selection = MediaStore.Audio.Media.DATA + "=? ";
String[] projection = new String[] { MediaStore.Audio.Media._ID , MediaStore.Audio.Media.ALBUM_ID};
Cursor cursor = context.getContentResolver().query(
audioUri,
projection,
selection,
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
long albumId = cursor.getLong(cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID));
Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
Uri imgUri = ContentUris.withAppendedId(sArtworkUri,
albumId);
Log.d(TAG,"AudioCoverImgUri = "+ imgUri.toString());
Glide.with(this)
.load(imgUri)
.into(((ImageView)findViewById(R.id.button)));
cursor.close();
}
}
I want to my application to scan for music files only in those folders which I have specified in preferences. So that other music files like audio books do not get included.
Currently, I've written this code, that scans all music files:
private String[] getTrackList() {
String[] result = null;
String[] projection = { MediaStore.Audio.Media._ID,
MediaStore.Audio.Media.DISPLAY_NAME,
MediaStore.Audio.Media.DATA, MediaStore.Audio.Media.TITLE };
String selection = MediaStore.Audio.Media.IS_MUSIC + "!=" + 0;
cursor = managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
projection, selection, null, MediaStore.Audio.Media.TITLE
+ " ASC");
result = new String[cursor.getCount()];
int position = 0;
for (cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE);
result[position] = cursor.getString(columnIndex);
position++;
}
return result;
}
Check out the answer to a similar question (although it is for images, the same idea should apply to audio):
Get filename and path from URI from mediastore
Looks like DATA stores the file path. I think if you use the MediaStore.Audio.Media.DATA column as part of your where clause, you can return only audio files from the directory you want.