Android ACTION_MEDIA_SCANNER_SCAN_FILE not showing image until reboot? - android

I am currently developing an Android Application where users take pictures which are stored in External Memory on the device then I am trying to also get the Gallery to scan the file to add the new media to the Gallery however this does not seem to update until the device reboots.
The code I am using is as follows:
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
File f = new File(mCurrentPhotoPath);
Log.v("INFO", mCurrentPhotoPath.toString());
Uri contentUri = Uri.fromFile(f);
mediaScanIntent.setData(contentUri);
this.sendBroadcast(mediaScanIntent);
After taking a picture with my app I can verify it exists using a file manager app on the device and also Logging the file path it appears to be correct before passing it to Media Scanner
file:/storage/emulated/0/Pictures/JPEG_20140712_163043_1418054327.jpg
Thanks Aaron

I managed to solve this. The issue was due to, where I setup the mCurrentPhotoPath. So I updated the code to use
photoFile = createImageFile();
mCurrentPhotoPath = photoFile.getAbsolutePath();

Media Scanner take some to scan and insert show results after calling sendBroadcast you can use MediaScannerConnection method onScanCompleted
and then check image in gallery.
for more reference follow Link

Related

In Nougat Capture Image from Camera and get file from Uri

In Android Nougat and above versions the method to capture image from camera intent is changed and following code is working fine for me.
Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
// File file = new File(AppGlobal.URI_CAPTURED_IMAGE.getPath());
AppGlobal.URI_CAPTURED_IMAGE = FileProvider.getUriForFile(parent, context.getPackageName() + ".provider", AppGlobal.getOutputMediaFile());
}
else
{
AppGlobal.URI_CAPTURED_IMAGE = Uri.fromFile(AppGlobal.getOutputMediaFile());
}
camera.putExtra(MediaStore.EXTRA_OUTPUT, AppGlobal.URI_CAPTURED_IMAGE);
camera.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivityForResult(camera, WritePostFragment.REQUEST_CODE_PHOTO_CAPTURE);
In onActivityResult method I'm able to show the captured image in ImageViews as well.
Glide.with(parent).load(AppGlobal.URI_CAPTURED_IMAGE).centerCrop().into(profilePic_iv);
But when i use the same uri to get File then it says no file exists at given path. What could be the issue? How can i parse the Uri to get File. It seems to be version related stuff.
Sample uri is as follows:
content://com.example.demo.provider/external_files/Camera/IMG_20171213_015646.jpg
How can i parse the Uri to get File
You don't. Moreover, you do not need to. Your file is whatever AppGlobal.getOutputMediaFile() returns. You need to hold onto that value (including putting it in the saved instance state Bundle, in case your process is terminated while the camera app is in the foreground). See this sample app for how to use ACTION_IMAGE_CAPTURE with FileProvider.

Gallery app crashes after my application saves image in folder

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!

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.

Thumbnail isn't refresh immediately

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.

Android phonegap app : unable to retrieve an image taken by the camera using my own code (not phonegap's)

Note- even though im using phonegap, the question is not about some issue in that.
Hi, im developing a android app. my app is using phonegap 1.3.
mi problem...
Im using phonegap apis to take a picture and display it in my app. But whats happening is due to some reason the os kills my app after the camera is launched, so that after the photo is clicked, my app is relaunched and it doesnt get the info abt the taken picture.
As a workaround to this problem (the problem), i designed a phonegap plugin which on app start checks if the app had crashed while taking a picture (some flags in code), and if it is restarting after crash, it retrieves the Pic.jpg taken by the camera and tries to displays it. The problem is that its not able to get the right image, or even a proper .jpg file for that matter.
mi code...
Phonegap makes an intent to start the camera and passes the uri for the Pic.jpg that it creates into that intent which it passes to startActivityForResult.
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(DirectoryManager.getTempDirectoryPath(ctx), "Pic.jpg");
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
this.imageUri = Uri.fromFile(photo);
this.ctx.startActivityForResult((Plugin) this, intent, 0);
Note- The above code is from the file CameraLauncher.java of Phonegap.
Now, im assuming that the startActivityForResult stores the picture the user captures at the file which was created by 'photo' in the above code. So even if after the os closes the app while the camera is open (refer - the problem) the photo will be stored there. PLEASE correct me if this assumption is wrong, or if the photo might be being saved somewhere else.
So taking into account this assumption i wrote a plugin which retrieves this image using the same logic which Phonegap uses in CameraLauncher.java
Uri imageUri;
ExifHelper exif = new ExifHelper();
exif.createInFile(getTempDirectoryPath(ctx) + "/Pic.jpg");
exif.readExifData();
File photo = new File(getTempDirectoryPath(ctx), "Pic.jpg");
imageUri = Uri.fromFile(photo);
Bitmap bitmap = null;
bitmap = android.provider.MediaStore.Images.Media.getBitmap(this.ctx.getContentResolver(), imageUri);
ContentValues values = new ContentValues();
values.put(android.provider.MediaStore.Images.Media.MIME_TYPE, "image/jpeg");
Uri uri = null;
try {
uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} catch (UnsupportedOperationException e) {
uri = this.ctx.getContentResolver().insert(android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI, values);
}
// Add compressed version of captured image to returned media store Uri
OutputStream os = this.ctx.getContentResolver().openOutputStream(uri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, os);
os.close();
exif.createOutFile(getRealPathFromURI(uri, this.ctx));
exif.writeExifData();
bitmap.recycle();
bitmap = null;
System.gc();
result = new PluginResult(Status.OK, uri.toString());
This is roughly the same logic as Phonegap uses. (I've removed all exception handling n all for posting. So assume there are no compile errors n all.) So, im basically trying to retrieve the Pic.jpg and return it to my phonegap app. BUT whats happening is that im getting a corrupted file of abt 150kb that isn't even a jpg (doesnt open).
Please tell me if its even possible to retrieve images in this manner after the activity that started the camera as died. And if its possible then, what am i doing wrong. Please help!
As I mentioned over in your other SO question,
phonegap android app crashing due to low memory on opening camera
the problem is most probably the Sony implementation of the camera intent. You should try testing by adding a third party camera app and when you take a picture select that app and see if it still crashes. It probably won't.
The issue here was because of some changed code in the FileTransfer plugin of Phonegap.
I was able to retrieve an image taken by the camera, after my app restarted after the crash using the above code only. :)

Categories

Resources