How to call system's media scanner? - android

My app downloads .mp3 files to download folder. It works fine, but then no music player app is able to play it ("cannot play this type of files").
I have to reboot every time to force the system to run media scanner, to make it work.
I tried MediaScannerConnection, but it does not work on my API level (24)
File sdCard = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
MediaScannerConnection.scanFile(this, new String[]{sdCard.getPath()},
null, new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
// now visible in gallery
Toast.makeText(getBaseContext(), "files have been scanned dawg", Toast.LENGTH_LONG).show();
}
});
It does not show any toast.
I have found another method, but I have heard that it breaks from android 4.4
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
This method breaks from android 7 apparently
File file = new File(absolutePath);
Uri uri = Uri.fromFile(file);
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri);
sendBroadcast(intent);
How do I make it work on api level 15-current? Is there any universal way? None of them worked on my device yet.

Related

How to make images I download within my app show in default gallery?

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.

Image not show in gallery

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.

Android Sharing Intent cannot start

I am writing an Android application to upload a mp4 format video from sdcard to youtube using sharing Intent. It ran perfectly at the first time when I installed to my phone. However, after I restart the app, I cannot share it again. If i share to whatsapp, "sharing fail" apear. If I click youtube, I got "no media uri(s)" message on logcat. Reinstalling on same phone or restarting the phone cannot solve the problem.
Even when I install the app in same code to other android device, it only runs perfectly once. When i restart the app, same problem happened.
I got the code from this website:
Trouble with youtube upload
And this is my code:
findViewById(R.id.button2).setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("video/*");
ContentValues content = new ContentValues(4);
content.put(Video.VideoColumns.DATE_ADDED,
System.currentTimeMillis() / 1000);
content.put(Video.Media.MIME_TYPE, "video/mp4");
content.put(MediaStore.Video.Media.DATA, Environment.getExternalStorageDirectory().getAbsolutePath()+"/myVideo/myVideoTemp.mp4");
ContentResolver resolver = getBaseContext().getContentResolver();
Uri uri = resolver.insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, content);
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_TITLE,"");
sharingIntent.putExtra(android.content.Intent.EXTRA_STREAM,uri);
startActivity(Intent.createChooser(sharingIntent,"Share video"));
}
});
p.s. there is no error in red color on logcat
Problem solved after adding sharingIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
Try the following to invoke the "share" applications:
//Use the URI that points to the video file, in my case, the video file is in local storage (sd card)
Uri fileUri = Uri.fromFile(videoFile);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
//Use the setDataAndType method to provide the URI and MIME type to the file
//Please note, you need to use setDataAndType method to make it work
intent.setDataAndType(fileUri,URLConnection.guessContentTypeFromName(fileUri.toString()));
startActivity(intent);
I have a blog to show how you can invoke video players from your app with a video file stored locally. It may help:
http://software.intel.com/en-us/blogs/2014/03/20/video-playing-with-android-media-player

How To Add Media To MediaStore on Android 4.4 KitKat SD Card With Metadata

Background: In Android 4.4, Google decided that apps should not have write access to SD cards.
However, apps can write to /SDCard/Android/data/App Package Name.
So this is what I've done. I have written an MP3 file to /Android/data/. I then want this MP3 file to show up in Android music players.
I've tried the following...
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ filePath)));
and....
MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, new String[] {"audio/*"},
new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri);
}
});
and...
ContentResolver.requestSync(CreateSyncAccount(context), MediaStore.AUTHORITY, null);
both of these will add the file to the MediaStore as a file with a URI like content://media/external/file/xxxxx
However, the file is not added as audio. I can update the MediaStore to declare the file as audio, but the file metadata isn't imported.
Other than write my own tag reader, how can I get the media scanner to scan the file and insert metadata like it does on the internal memory and pre-KitKat SD cards?
I've given up looking for a solution. I do not think there is one, so I've filed an issue report.

Scan Android SD card for new files

My app allows a user to save an image to their SD card. But I'm not sure how to make it appear in the gallery until you unmount and remount the SD card. I have googled for a couple of days with this problem but am not sure how to make it appear automatically. I found
this link but I'm not sure how to use the class. This is what i use to save the file. At the bottom of the try catch block is where I want to scan the sd card for new media.
FileOutputStream outStream = null;
File file = new File(dirPath, fileName);
try {
outStream = new FileOutputStream(file);
bm.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();
} catch {
...
}
If anyone could point me in the right direction, I would appreciate.
I've tried plenty of different methods to trigger the MediaScanner, and these are my results.
SendBroadcast
The most simple and naive solution. It consists in executing the following instruction from your code:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
However, this no longer works in KitKat devices, due to a lack of required permissions.
MediaScannerWrapper
As posted here (per #Brian's answer), it consists in wrapping a MediaScannerConnection instance in order to trigger the scan() method over a specific directory. This method has proven to be working fine for 4.3 and below, but still no luck with KitKat (4.4+).
FileWalker
One of the many Play Store apps that tries to overcome the MediaStore's lack of commitment to update its database is ReScan SD. It sends a lot of different broadcasts:
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file://" + Environment.getExternalStorageDirectory())));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable/SD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable/MicroSD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///mnt/Removable/MicroSD")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///mnt")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///storage")));
sendBroadcast(new Intent("android.intent.action.MEDIA_MOUNTED", Uri.parse("file:///Removable")));
and tries to support KitKat by manually triggering the scan() method over each file of the base directory. Unfortunately, this is both very CPU-intensive and time-consuming, so it is not very recommended.
"The shell way"
The only thing that seem to work with KitKat in some cases is sending the broadcast via adb shell. So, this snippet allows you to do just that programmatically:
Runtime.getRuntime().exec("am broadcast -a android.intent.action.MEDIA_MOUNTED -d file://" + Environment.getExternalStorageDirectory());
It is more of an hack-ish way of doing it, but at the moment is the best I could come up with.
Bottom line
Each of the above solutions actually works for everything that is not KitKat. That's because, thanks to Justin, a bug has been found and issued to the official Tracker. This means that, until the bug is ironed out, we are left with no true KitKat support.
Which one to use? Among those, I would use the MediaScannerWrapper solution, together with the shell-ish approach (the last one).
Since the last answer I posted apparently wasn't an appropriate method, I found another method here. You basically create a wrapper class, initialize it, and then call the scan() method. Very helpful post. Let me know if this isn't appropriate either.
Use MediaScannerConnection:
http://developer.android.com/reference/android/media/MediaScannerConnection.html
It can be a little bit of a pain because of the multiple levels of asynchronous calls, so as of API 8 (Froyo) there is a helper function:
http://developer.android.com/reference/android/media/MediaScannerConnection.html#scanFile(android.content.Context, java.lang.String[], java.lang.String[], android.media.MediaScannerConnection.OnScanCompletedListener)
You could also call media scanner explicitly by sending broadcast.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri
.parse("file://"
+ Environment.getExternalStorageDirectory())));
Edit
This was an old post. Updating it to new versions
Android is taking steps to prevent apps from spoofing more system broadcasts like this.
If you want to tell Android to index a file you put on external storage, either use MediaScannerConnection or ACTION_MEDIA_SCANNER_SCAN_FILE
Reference: This post
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
final Intent scanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
final Uri contentUri = Uri.fromFile(outputFile);
scanIntent.setData(contentUri);
sendBroadcast(scanIntent);
} else {
final Intent intent = new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory()));
sendBroadcast(intent);
}
If the above piece of code is not working you can try the following:
MediaScannerConnection.scanFile(this, new String[] {
file.getAbsolutePath()
}, null, new MediaScannerConnection.OnScanCompletedListener() {
#Override
public void onScanCompleted(String path, Uri uri) {
}
});
Here is another way to force scan:
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE,"uri to file"));
And then system will fire ACTION_MEDIA_SCANNER_FINISHED broadcast so you can react on it with BroadcastReceiver
In order to be able to receive ACTION_MEDIA_SCANNER_FINISHED broadcast, intent filter should contain data scheme:
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_SCANNER_FINISHED);
intentFilter.addDataScheme("file");
context.registerReceiver(mMediaScannerFinishReceiver, intentFilter);
from the doc:
public static final String ACTION_MEDIA_SCANNER_SCAN_FILE
Added in API level 1 Broadcast Action: Request the media scanner to
scan a file and add it to the media database. The path to the file is
contained in the Intent.mData field.
and
public static final String ACTION_MEDIA_SCANNER_FINISHED
Added in API level 1 Broadcast Action: The media scanner has finished
scanning a directory. The path to the scanned directory is contained
in the Intent.mData field.
You can use MediaStore.Images.Media.insertImage to insert an item into the gallery.

Categories

Resources