I am developing an Android app for Jellybean, KitKat and Lollipop and several different devices.
At the beginning of project, I used the Jellybean API library with target API 'anroid-18' using 'Samsung galaxy note 2014 edition'.
A few months later, the OS of the device started upgrading to KitKat OS, so I changed the library to 'android-19' in which version number is 4.4.2.
I have new device named Galaxy S tab 2 now.
I have the three versions and two devices now and my default API is 4.4.2.
The problem occurred when capturing pictures using the app:
if (Build.VERSION.SDK_INT >= AlopexBuild.VERSION_SUPPORT.KITKAT) {
Intent mediaScanIntent
= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.parse("file://"+ path);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
} else {
sendBroadcast(
new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ path)));
}
The following code has no problem in Jellybean:
Intent mediaScanIntent
= new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.parse("file://"+ path);
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
Why is this happening?
Jellybean OS + Kitkat API = Failed
Kitkat OS + Kitkat API = OK
Lollipop OS + KitKat API = OK
Problematic code is
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ path)));
According to the wiki articles references for each OS when releasing the new KitKat and Lolipop OS and moving from Jellybean they changed to a new kind of runtime enviroment. The code only compatible in this new enviroment, you'll need to try a different method for other enviroments like Jellybean.
See here and here
Related
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.
I'm getting: android.os.FileUriExposedException.
When targeting Android N, file:// URIs are not allowed anymore. I know
We should use content:// URIs instead. However, my app needs file for
both image and video. Any ideas?
mMediaUri = Uri.fromFile(new File(AppHelper.getDirectoryPath(),AppHelper.getFileName() + ".jpeg"));
Intent iCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
iCamera.putExtra(MediaStore.EXTRA_OUTPUT, mMediaUri);
startActivityForResult(iCamera, Constants.INTENT_CALL.CAPTURE_IMAGE);
and onActivityResult
case Constants.INTENT_CALL.CAPTURE_IMAGE:
String filePath=SiliCompressor.with(getActivity()).compress(mMediaUri.toString(), true);
Please add sample code...if Available.
After some research finally got relevant answer to my question just set min target sdk version to 23.
My app allows users to take a picture with the camera and saves it to a custom folder. This works fine. The issue being seen by users (Samsung devices) is that the Gallery app crashes when launched. The only way to fix this is to go into Pictures folder on the device and remove the myApp custom directory.
Has anybody seen this issue before? I can paste the image creation/saving code if needed but that is working just fine.
Is this a permissions issue?
Could this be the issue?
You can try update system according to system version:
File mediaStorageDir = new File(getPictureDirectory());
Uri contentUri = Uri.fromFile(mediaStorageDir);
if(Build.VERSION.SDK_INT >= 19){
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(contentUri);
getApplication().sendBroadcast(mediaScanIntent);
}else{
getApplication().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, contentUri));
}
It works for me. Hope it help!
I am very new to Android development!
I am working on a camera-related app, and I encountered a problem that the pictures taken are not immediately displayed in 'Photos' app in Android 5.0.1, but when I restart the phone, and take a picture one more time, the previous pictures would all be displayed in 'Photos' app. And this problem only occurs in Android 5.0.1. I tried it on Android 4.3, and there isn't the problem.
And I do add the following codes in my program:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Intent mediaScanIntent = new Intent(
Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
Uri contentUri = Uri.fromFile(new File("file://" + Environment.getExternalStorageDirectory() + "/DCIM")); //out is your output file
mediaScanIntent.setData(contentUri);
sendBroadcast(mediaScanIntent);
}
Could anybody help me out there please????
My application shows the list of songs in sdcard, and there is an option to delete the song from SD card.
Even when the song is deleted the song still comes in my applications list.
How can I update the android media database and show the updated database?
Android has a cache of sorts that keeps track of media files.
Try this:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
It makes the MediaScanner service run again, which should remove the deleted song from the device's cache.
You also need to add this permission to your AndroidManifest.xml:
<intent-filter>
<action android:name="android.intent.action.MEDIA_MOUNTED" />
<data android:scheme="file" />
</intent-filter>
For Android before ICS, send a ACTION_MEDIA_MOUNTED broadcast :
context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
For ICS and Jelly Bean, you can use the MediaScannerConnection API to scan media files
Gallery refresh including Android KITKAT
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File("file://"+ Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES));
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
}
else
{
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
The below was tested only on emulators.
Solution works on 2.2 and 2.3, but not on 4. On 4th Android emulator this action does nothing with message in LogCat:
Permission Denial: broadcasting Intent
Unfortunately did not found the way to update media storage for Android 4
Did not test on Android 3.
The error happens also in the 2.3.3 Intel image. Have not check on real devices.