how to update thumbnails preview of android gallery after image edited - android

possible duplicate
Refreshing the thumbnail using MediaScanner
I have a problem regarding the update of thumbnails in the android in built-in gallery. Actually I have opened, edited and saved the image which was selected in the gallery.
I open leave the gallery and open the gallery again, then the thumbnail for that image is not updated. However, I pick the image, it is the edited version indeed.
Does anybody have solution about this? I have search but I'm getting the answer about refreshing the gallery using sendBroadcast(), which does not seem to update the image previews.
Edit
I don't want to delete the existing file because suppose user want to save file with different name then that file did not affected

Two suggestions.
Try getThumbnail. From the javadoc, I would expect it to regenerate an up-to-date thumbnail.
If that doesn't update the thumbnail, try deleting the thumbnail.

I found a workaround for this problem, before you request a new thumbnail you need to delete old one. Use MediaStore and ContentResolver for this
private static void removeThumbnails(ContentResolver contentResolver, long photoId) {
Cursor thumbnails = contentResolver.query(Thumbnails.EXTERNAL_CONTENT_URI, null, Thumbnails.IMAGE_ID + "=?", new String[]{String.valueOf(photoId)}, null);
for (thumbnails.moveToFirst(); !thumbnails.isAfterLast(); thumbnails.moveToNext()) {
long thumbnailId = thumbnails.getLong(thumbnails.getColumnIndex(Thumbnails._ID));
String path = thumbnails.getString(thumbnails.getColumnIndex(Thumbnails.DATA));
File file = new File(path);
if (file.delete()) {
contentResolver.delete(Thumbnails.EXTERNAL_CONTENT_URI, Thumbnails._ID + "=?", new String[]{String.valueOf(thumbnailId)});
}
}
You can get photoId from its URI, to get URI from file name just create File and parse URI from it
Uri uri = Uri.fromFile(file);

Related

How to write file into DCIM directory exactly where camera does?

Suppose I am writing an alternative Camera application and wish to write images exactly into the same place as Camera does and name them exactly in the same name Camera does.
How would I accomplish this?
How to know the location of camera files?
How to know current naming convention?
How to gain permissions to that directory?
Any of answer would be appreciated.
Okay, suppose it is not really camera alternative. Suppose I would like to write formats other than images, like audio, video, or something else.
How would I accomplish this?
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM) + File.separator + "You Dir. Name";
if you append any string to end this
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
it will create dir inside the folder
How to know the location of camera files?
By default camera uses this location
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM)
How to know current naming convention?
IMG_yyyyMMDD_timeStamp
How to gain permissions to that directory?
Using permission manager for Camera and External Storage permissions
You cannot write exactly into the same folder as the default camera app does. But you can make use of Environment.getExternalStoragePublicDirectory(Environment.DI‌​‌RECTORY_DCIM)
mediaStorageDir = new File(Environment.getExternalStorageDirectory(), FOLDER_NAME);
Intent takePictureFromCameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureFromCameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
Uri.fromFile(mediaStorageDir));
startActivityForResult(takePictureFromCameraIntent, 100);
But, please note that this will create only a sub-folder in the DCIM directory and will not store exactly where the default camera does. But you can always create sub-folders with your required folder name.
Things to keep in mind for this answer:
Every phone producer creates it's own Camera App, tailored to their hardware.
With the right permissions, App's can write (almost) everywhere they want...
Now to your question(s):
First, we don't know where the photo's are stored, and what the naming convention is. Every one and their mother has a different idea about what is best. So no "Hey it's always there".
Those who seek will find: get read & write permissions for the whole device. With that search the whole device to find in what folders the images are in. Now subtract the folders that come from "social media". My guess would be that the folder with the most and or latest images is the one that you want. To be sure, you will need testers, that trust you.
All that are found were not unorganized: just find the pattern used. There may be a standard for this. The big companies will surely have one. You can ask the device what maker it has. Note, that that answer might not be correct.
Ain't that a picture, no, it's a photo: And then you get the fun part of accessing the camera. Good times. Remember: request picture => get location of picture in memory => save picture as file. Best part, there is no enforcement of all parts of this API, so different devices need different instructions...
Have fun & Good luck!
ps, the downvotes are probably for the lack of code
Try using this code you can save the image bitmap to the directory using insert query
String imgSaved = MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap,
System.currentTimeMillis() + ".jpg", "DESCRIPTION HERE");
For more details see the link
I've found this code useful for choosing the last used DCIM/Camera folder.
String getDCIMCamera() {
try {
String[] projection = new String[]{
MediaStore.Images.ImageColumns._ID,
MediaStore.Images.ImageColumns.DATA,
MediaStore.Images.ImageColumns.BUCKET_DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATE_TAKEN,
MediaStore.Images.ImageColumns.MIME_TYPE};
Cursor cursor = getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
null,
null,
MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC");
if (cursor != null) {
cursor.moveToFirst();
do {
String path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA));
if (path.contains("/DCIM/")) {
File file = new File(path);
path = file.getParent();
cursor.close();
return path;
}
} while (cursor.moveToNext());
cursor.close();
}
}
catch (Exception e) {
}
return "";
}

Not getting all media files which inserted manullay in sd card

I used below query to load all pictures or video from sd card and it works accordingly. But when I add some video or pictures manually into sd card at different folder then its not loading after query. Please suggest me regarding same.
final String[] columns = { MediaStore.Video.Media.DATA,
MediaStore.Video.Media._ID};
final String orderBy = MediaStore.Video.Media.DATE_TAKEN;
Uri videosuri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
Cursor imagecursor = getContentResolver().query(videosuri, columns, null, null, orderBy);
if (imagecursor != null && imagecursor.getCount() > 0) {
while (imagecursor.moveToNext()) {
int video_id=imagecursor.getInt(imagecursor.getColumnIndex(MediaStore.Video.Media._ID));
int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Video.Media.DATA);
String path=imagecursor.getString(dataColumnIndex);
}
}
When you "add some video or pictures manually into sd card at different folder", the MediaStore has to be updated in order for them to be available to your query, as well as to other apps that use the MediaStore backend.
Adding them via MTP or apps like Gallery will invoke the MediaScanner (or some similar process) to add them to the MediaStore, but adding them via adb push or in your own code requires you to explicitly do so afterward.
In your code, after writing an image or video file to the SDCard, you can pass the path and MIME type of the file to the MediaScanner by implementing the MediaScannerConnectionClient interface in a class, instantiating it, then calling scan(). The MediaScanner will then open the file, collect/generate metadata, and add the file to the MediaStore
See android - dynamically add pictures to gallery widget for an example class using this approach.

Apppic hide/disable from galary and file manager but it must showing in my application

I have an application which take picture and save it in folder in sd card, after I upload it to server, my requirement is my pic taken from the application should not get displayed in device gallery as well as file manager (except some HD app) but after hiding the folder / images it must shown in my application.
I have used some solutions like adding "." before any folder name, but using this will disappear my pic from my application too so it is useless,
I also used .nomedia file concept but it is not working properly, images show in file manager.
Any help will be appreciated...
Indeed a .nomedia file in a folder will not show pictures from that folder in Gallery app. But you can never prevent a file explorer to display them. add "." before folder name it will dispear pic my application too so it useleass. That is big non sense as then you are doing something wrong in your apps code. But even then all filemanagers would display it (as all file managers will display hidden files wenn told).
The best to do is to put the files in a folder in internal memory. Then a filemanager can not see them. (At least when the phone is not rooted).
thanking you in advanced to use your time.... I hate such words.
#greenapps for add just "." in folder name it totaly hide the image from everywhere (from gallery default app as well my app to ).
but Thanks for spend your thought i found my solution for hide the image from gallery app only
here i just call this function immediate after save a pic in my app folder,
this function delete the latest pic from the default DCIM folder but yes you have to validate that if the picture is taken or not cause ;-)
if you find anything wrong kindly notify me!
public void deleteImageFromGallery(String captureimageid) {
Cursor c = null;
Uri u = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=?",
new String[]{captureimageid});
String[] projection = {MediaStore.Images.ImageColumns.SIZE, MediaStore.Images.ImageColumns.DISPLAY_NAME,
MediaStore.Images.ImageColumns.DATA, BaseColumns._ID,};
AppLog.i("InfoLog", "on activityresult Uri u " + u.toString());
try {
if (u != null) {
c = managedQuery(u, projection, null, null, null);
}
if ((c != null) && (c.moveToLast())) {
ContentResolver cr = getContentResolver();
int i = cr.delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, BaseColumns._ID + "=" + c.getString(3),
null);
AppLog.i("TAG", "Number of column deleted : " + i);
}
} finally {
if (c != null) {
c.close();
}
}
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));
}

Update MediaStore audio tags will not work

I am trying to edit Mp3-Information of the MediaStore.
All the edited Mp3 Songs do not have an album name.
ContentResolver res = FindCoverActivity.this.getContentResolver();
Uri sSongUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Uri uri2 = ContentUris.withAppendedId(sSongUri, song_id);
ContentValues values2 = new ContentValues();
values2.put(MediaStore.Audio.Media.ALBUM_ID,"9999"); //new album_id. I dont know how to get an unused id..
values2.put(MediaStore.Audio.Media.ALBUM, "test_album");
Integer as = res.update(uri2, values2,null,null);
if (as != 0) {
// got updated
}
This code does not work. The result is a messed up mediastore..
Does anyone know more? Thanks!
There is an android table called albums which has the following colmns:
album_id (Integer)
album_key (Text not null)
album (text)
both album_id and album_key are maintained by the android system so do not worry about them.
If I understand your problem, you need to find a specific album and then edit the albumname. You must have these in a specific folder structure otherwise you could not identify an album.
Why not use a pc and some mp3 tagging tool such as Mp3 Tag Tools. This also allows you to add albumart etc.
Hope this helps.

Android album art thumbnails problem: resolveUri failed on bad bitmap uri

I am trying to display a list of albums (of music mp3s stored on the sd-card) along with the album art for each album. The album art is stored in the mp3s tag (it is not a separate image on the SD card)
I can get the album details fine using:
String[] projection = new String[] { Albums._ID, Albums.ALBUM, Albums.ARTIST, Albums.ALBUM_ART, Albums.NUMBER_OF_SONGS };
String rowsToReturn = null;
String[] selectionWhere = null;
String sortOrder = Media.ALBUM + " ASC";
Cursor cursor = contentResolver.query(Albums.EXTERNAL_CONTENT_URI, projection, rowsToReturn, selectionWhere, sortOrder);
String albumArtUri = cursor.getString(cursor.getColumnIndex(Albums.ALBUM_ART));
The URI is correct, but when I try to set the URi to the src of an ImageView, I get a silent error being thrown "resolveUri failed on bad bitmap uri".
imageview.setImageURI(Uri.parse(albumArtUri));
This only happens for some albums. Some are fine and display the album art correctly.
I took a look at the URI path and checked on the SD card for the thumbnail images and found them here: /mnt/sdcard/Android/data/com.android.providers.media/albumthumbs/
I found images that work and images that dont, so the image does exist, but just cannot be read correctly. I even tried copying the images off the sd card and adding .png extension to the files - the ones that show up on the phone work, but the ones that don't are 'corrupt'
Finally I copied the mp3 files themselves back to the PC and inspected the album art embedded in the tags and they all show correctly.
It's almost as if the thumbnails that the Android system is creating form the tags are invalid or corrupt, but this can't be right.
Or maybe I need to read them differently?
i think the issue could be with Media scanning. Delete the cache folder and open the music player app.It will create album cache again for all the songs. See if this helps.

Categories

Resources