Image not added to Gallery - android

I have followed tutorials and Stackoverflow's questions without any success.
What I have to do is: take a picture from Camera, save it and show it in the gallery.
I don't know why with the following code, the image is not added to the gallery. The code doesn't give any error and I have checked with some Log.i that it is fully executed. But the problem remains: the picture is taken but not shown into the gallery.
Because of my code is really long, I report only the key parts in order to make the reading easier. I hope that it is clear enough.
File mFile = new File(Environment.DIRECTORY_PICTURES, "pic.jpg");
mBackgroundHandler.post(new ImageSaver(reader.acquireNextImage(), mFile)); // it saves the image
Uri contentUri = Uri.fromFile(mFile);
getActivity().sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, contentUri));

Related

MediaStore.Media.Images Android/ Kotlin: How to hide photos saved in gallery from user?

I am using the following program to capture an image using the MediaStore.ACTION_IMAGE_CAPTURE cameraIntent.
Code:
val values = ContentValues()
values.put(MediaStore.Images.Media.TITLE, "New Picture")
values.put(MediaStore.Images.Media.DESCRIPTION, "From the Camera")
imageUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
//camera intent
val cameraIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri)
startActivityForResult(cameraIntent, IMAGE_CAPTURE_CODE)
I have the following questions:
The image that has been captured is being shown in the gallery. How do I hide that from the user?
After taking the image, I am setting the photo in a ImageView that is lagging the activity. How can I compress the image when I am setting it to the ImageView?
Is there a way to click photos only in potrait mode?
I tried changing the EXTERNAL_CONTENT_URI to INTERNAL_CONTENT_URIbut that gives me the following warning: Writing to internal storage is not supported android
As soon as you use the media store they are in 'the gallery'.
So you have to use getFilesDir() and classic file methods to save them for your eyes only.
The image that has been captured is being shown in the gallery. How do
I hide that from the user?
As blackapps have said, and in accordance to this documentation, which states that MediaStore API uses shared storage, you can't use MediaStore API to store in app-specific storage (Not the Gallery).
This documentation provides an example of storing photos to app specific storage using getExternalFileDir().
After taking the image, I am setting the photo in a ImageView that is
lagging the activity. How can I compress the image when I am setting
it to the ImageView?
Try Glide, a fast and efficient image loading library for Android. In addition, as I'm typing this, CommonsWare has also recommended other Image loading libraries such as Picasso and Coil.
Is there a way to click photos only in portrait mode?
Perhaps this SO Q&A might help. You can set theclickable attribute of the ImageView programmatically or declaratively.

Android - Generate Thumbnails in Gallery

I am currently creating a camera app and storing it. However, every time I take a picture, the picture is saved in the directory but is not saved in the Gallery. I think it's all about the generation of thumbnail right after the image has been taken. How can you generate a thumbnail in the gallery?
Try this:
Intent mediaScan = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScan.setData(uri);
context.sendBroadcast(mediaScan);
If you are adding files or images programmatically, you have to use MediaScannerClass to scan the media file for your sd card.
of you can also use following piece of code:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

Android Java: Get Bitmap in Gallery

I have a Bitmap saved in the directory: Environment.getExternalStorageDirectory()+ File.separator + "Images" in a subdirectory. With a fileManager i can find the image and open it. But in the Gallery of the Smartphone i can't find the Image.
How can i get it there?
To be make you file known by the Gallery you need to call the MediaScanner:
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
See also: https://www.grokkingandroid.com/adding-files-to-androids-media-library-using-the-mediascanner/
You can actually directly access the Pictures directory. To do this, use
getExternalStoragePublicDirectory(String type)
, where type is equal to Environment.DIRECTORY_PICTURES.
See http://developer.android.com/reference/android/os/Environment.html#getExternalStoragePublicDirectory%28java.lang.String%29 for more info.
That's cleaner than supposing that pictures are always stored in a directory called "Images"...
Now to force the Gallery to register the change, look at https://stackoverflow.com/a/15837638/2984973 .
To quote the answer, you want to broadcast an intent specifying your new picture, likewise:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(myNewFile)));

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