I am showing all available videos in a gridView on which are in SDCard by using following code.
String[] proj= {MediaStore.Video.Media._ID,MediaStore.Video.Media.DATA,MediaStore.Video.Media.DISPLAY_NAME,
MediaStore.Video.Media.SIZE };
videocursor = getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, proj, null, null, null);
It is working fine... But if i made any changes to videos (i.e rename, delete) above code is not working. It is showing old content only, means not refreshing. How can i solve this problem
what should do is the following :
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
right after your code .
provides a way for applications to pass a newly created or downloaded media file to the media scanner service.
Related
I made an application that has as one of it's features file sharing, and the files sent may be anything. However, Images and Videos sent are not showing up on the default Gallery app using the com.androidquery.AQuery download. Is there a step I'm missing that would mark the file as media or something like that? Because I thought you only needed to mark file as NOT media on Android when you really don't want them to show.
After file downloading you need to execute
private void addImageGallery(File file) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, file.getAbsolutePath());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
}
To register your file in android MediaStore where gallery takes data about stored media files
After some research based on Sone's answer, I got this code I needed to insert after the download:
Uri uri = Uri.fromFile(new File(downloadFilePath)); //Insert your file path here
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(uri);
getApplicationContext().sendBroadcast(mediaScanIntent);
As he said, I needed to register my file in android MediaStore, and this code does that no matter what type of media is downloaded by forcing the scanner to go over the recently downloaded file. Hope it helps anyone else needing this.
I launch default camera using intent and store those camera images in external storage using below path:
File file = new File(Environment.getExternalStorageDirectory()
+ File.separator + fileName);
But it does not show in gallery. The issue comes in nexus 4,7 and moto G devices with OS 4.4.2
I try with
mContext.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://" + Environment.getExternalStorageDirectory())));
But It not work
You will have to refresh the media scanner cache, try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(yourFile)));
You have to tell the device to scan the new media file. Try with this snippet:
MediaScannerConnection.scanFile(
this, new String[]{file.getAbsolutePath()}, null, null);
I think you'd better make a folder in the camera default folder and put your data in it. Check if a file with the name of ".nomedia" is not in that folder.
I'm making a file manager in which picture items have a small thumbnail.
I get thumbnail image by using MediaStore. Everything works fine. But when I rename or move a file, the thumbnail does not show up.
I've found a piece of code to refresh MediaStore:
getActivity().sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
It worked but I must wait 4 or 5 second and refresh, then the thumbnail updates.
How to get thumbnail of image immediately after rename or moving?
What happen if you use ACTION_MEDIA_SCANNER_SCAN_FILE instead of ACTION_MEDIA_MOUNTED, (i.e. trigger a refresh for a single file instead of for the complete directory hierarchy) ?
You will need to replace the URI of the directory with the URI of the file, obtained for example using Uri.fromFile().
When you move or rename a file you should refresh the old and the new URIs.
The recommended way to update one specific image in Android is using ACTION_MEDIA_SCANNER_SCAN_FILE intent. And for smoother
You can check it at Basic Photo Handling Training in Android Developer Site.
private void galleryAddPic() {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
If you want to show new thumbnail immediately for some missing files, you can do it by yourself. First, check the MediaStore as before, and if the returned thumbnail is null then generate your own one using ThumbnailUtils or BitmapFactory.
And, For handling a bitmap and displaying it, there is a quiet straightforward sample in Android Training Course.
Have you tried doing the scan directly on the directory you are changing? So instead of
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
something like
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/path/to/your/folder")));
An alternative would be to manually use ThumbnailUtils.
Actually sending Intent.ACTION_MEDIA_MOUNTED broadcast intent is really ugly. Read this post http://androidyue.github.io/blog/2014/01/19/scan-media-files-in-android As to renaming files. You should remove the older file from the library and then add the new one into the library. I think this could help you.
I have an application that makes changes to some of the files in the media folders (DCIM/Camera specifically)
After I make theses changes the application sends this broadcast in order to force the MediaScanner to run so that my changes get reflected in the Gallery app the next time it is opened.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This works perfect on devices running stock android. However on devices with motoblur it fails and gives me this output in the log:
Permission Denial: broadcasting Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///mnt/sdcard } from com.my.package (pid=20882, uid=10109) requires com.motorola.blur.service.blur.Permissions.INTERACT_BLUR_SERVICE due to registered receiver BroadcastFilter{40a444c8 ReceiverList{40a22888 13696 com.motorola.blur.service.blur/10023 remote:40a340b8}}
Is there some way I can use this INTERACT_BLUR_SERVICE permission? Or is there some other way I can get the Media Scanner to run on command?
Souldn't adding this permission in your AndroidManifest solve the problem?... like this :
<uses-permission android:name=
"com.motorola.blur.service.blur.Permissions.INTERACT_BLUR_SERVICE"/>
I never found a way to trigger the media scanner. But I was pointed towards a different means of deleting the images and videos, rather than just deleting the files on the SD card, I now use a ContentResolver to delete the media.
Here is a snippet of how I've done it:
//Uri imgUri = Uri.parse("content://android.provider.MediaStore.Images.Media");
ContentResolver cr = getContentResolver();
int count = cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null);
count += cr.delete(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, null, null);
Log.i(myTag, "Deleted " + count + " files from media database");
Where would I get started to create an app that would grab your Album Art based on Album or Artist of the mp3 from the internet (ie. via amazon, play.com, etc..)?
I would likely be using Eclipse.
Cusor cur = getContentResolver().query(Albums.EXTERNAL_CONTENT_URI, new String[] {Albums.ALBUM_ART}, Albums._ID + "=?", new String[] {albumid}, null);
Album Artist is still undocumented and not available before 2.3.3, I believe.