Load Images folder in android gallery - android

I am working on an application, where i take pictures from a camera and store it in a separate folder in sd card. The problem what i am facing is, once the pictures are stored in a folder on sd card the Image folder is not being displayed in android native gallery.
This should be something similar to whatsapp image folder. how is it possible?

use the function MediaStore.Images.Media.insertImage to create a thumbnail and show the image in the default android gallery.
String imageName = "imagepath";
File sdImageMainDirectory = new File(imageName);
MediaStore.Images.Media.insertImage(getContentResolver(),
sdImageMainDirectory.getAbsolutePath(),sdImageMainDirectory.getName(), "description");

You need to let the system know that new media has been added before it will show up. Call this after you save the image.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Related

Cordova app downloaded images showing in gallery

My application downloads a set of images and stores them in the internal storage by creating a folder.
The problem is the images are being shown in the gallery. This should not happen, is there a way to programatically hide the images but still be usable by the app?
Thanks in advance.
EDIT:
This is only happening to the android version of the app. In the iOS it is not showing in the Camera Roll.
This depends where you download the images: If you download them on your Pictures folder or inside of a public folder from sdcard/internal memory then MediaScanner scan them so your pictures will be visible in the Gallery app.
To avoid this you can try one of the following solutions:
Add a .nomedia file inside your destination folder
OR
Download your pictures in app cache directory by using LocalFileSystem.TEMPORARY.
Something like
window.requestFileSystem(LocalFileSystem.TEMPORARY, 0, function(fs) {
fs.root.getDirectory("media", {create: true}, function(fileDirectoryEntry) {
// var destFile = fileDirectory + "/" + filename;
// downloadPicture(srcUrl, destFile);
});
});
The downside of this solution is that when the app is removed, your pictures are deleted as well.

Saving photos to DCIM/Camera - why aren't they showing up in Photos app?

I've got a custom camera app.
I'm saving the images (after doing some processing) using a FOS, writing the jpeg bytes I've gotten after using OpenCV Mats to manipulate the image:
File photo=new File(dir, getFileName()); // [external_storage]/DCIM/Camera/id.jpg
FileOutputStream fos=new FileOutputStream(photo.getPath());
fos.write(jpeg[0]);
fos.close();
Using a file manager, I can see that the images are saved correctly to the DCIM/Camera directory, along with photos taken using the Google camera app. However they are not shown in the Photos app on the phone. Do I need to set some kind of EXIF data for this to happen, or is there something else going on?
After the photo has been saved, add this line to let the Photos app know there are new photos:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + photo.getAbsolutePath())));
What is being missed is that the gallery is not refreshed after saving the files.
here is a small snippet
sendStickyBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
and add permisions in androidmanifest.xml

Insert video to android gallary

i need to Know how to insert video to android gallery or make my own folder in gallery to shows my app photos and videos
for example if i have my photo pathe like this
File file = new File(storagePath, FileName);
String MyPath= ""+Uri.fromFile(file);
what is the next step ?

not always getting the right image

I have a custom camera that has a public method for getting the thumbnail of that last image saved to a specific folder on the sdcard...
that method looks like this:
public void getGalleryThumb(){
// TODO add Logic for gallery images..
File sdDir = new File("/sdcard/LC/images");
File[] sdDirFiles = sdDir.listFiles();
if(sdDir.length()>0){
File lastPhoto = sdDirFiles[0];
Bitmap myBitmap = BitmapFactory.decodeFile(lastPhoto.getAbsolutePath());
//SET MY IMAGE VIEW BITMAP TO LAST FILE IN sdDIRFiles
photo.setImageBitmap(myBitmap);
btn_gallery.setVisibility(View.VISIBLE);
}
//Toast.makeText(getBaseContext(), "num images in gal:"+sdDirFiles.length +"last image name: "+sdDirFiles[0], Toast.LENGTH_LONG).show();
}
i have noticed that if i delete a photo from that folder the method above does not always retrieve the right image.. I have used:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory()+"/LC/images/")));
upon deleting and writing new files to that sdcard/folder but it doesn't seem to do the trick plus it forces this annoying toast message about the sdcard being mounted..
any help would be appreciated
I was writing this in a comment, but I'll put the detail here. Multiple issues:
listFiles doesn't not guarantee order. You'll need to sort your files by last modified.
You shouldn't access "/sdcard" directly. Your second snippet of code has Environment.getExternalStorageDirectory(). Use that.
You get messages about the SDCARD? Where are they coming from? If you mount it to your local machine, you won't be able to access it from your app while its mounted.

android , refresh image thumbnails , or sd card before calling an intent to choose an image

basically im trying to refresh the sd card in order to have image thumbnails represent their corresponding pictures. The reason for this is a user can send a picture to the server and the server performs image processing on it and then returns it. It shows up in the gallery as expected the first time. Subsequent requests however result in the first thumbnail always being used for the new image. The new image always overwrites the old one (by design) but the thumbnail stays the same.
currently im trying this to refresh the sd card
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
It has not worked however. Based on my implementation below is there anyway I could refresh the sd card before the select image intent is started ?
//if the user selects the file browser
//open the file browser and present the available images
if(v == choose){
//refresh the sd card before viewing the images
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
startActivityForResult(new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI), SELECT_IMAGE );
EDIT: I have also tried this http://www.mail-archive.com/android-developers#googlegroups.com/msg24164.html but to no avail
EDIT 2: I have also tried deleting the files before saving the new ones but its no use the thumbnails just stay the same
Edit 3: The device im using is a htc hero , I have also tried it on an lg gt540 and the problem did not occur as the thumbnails refreshed when overwritten , this leaves me to believe it may be a bug with htc's implementation . Has anyone else experienced this ?
In order to refresh the thumbs just rename the folder that the files are located

Categories

Resources