How to show list view choosed images in gallery android? - android

I am using list view and gallery in android.. i have 3 views as list items i.e a contact photo,name and a button to add that contact . when user clicks the button inside list view,the contact photo of that items should add in gallery... Can anybody please tell how to achieve this?

#Nidhi can you add image like that
private void addPicToGallery() {
Intent media = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
//mCurrentPhotoPath is the path to image.
File f = new File(mCurrentPhotoPath);
Uri contentUri = Uri.fromFile(f);
media.setData(contentUri);
this.sendBroadcast(media);
}

save image in sdcard. But after this, you will not be able to see that image in gallery. To see this image, You will have to refresh sdcard using
broadcast(sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory().getAbsolutePath())));)
when you will save this image.

If my understanding is correct, Why can't you just download the image and save them in sd card? It will show up in gallery automatically. if it is not showing in gallery you can call the following to call implicitly after downloading the images to sd card.
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));

Related

Image not added to Gallery

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));

Android display images from a specific folder

I need to display the images that are in a specific folder on the SD card.
I want to do it with a button and display the gallery. But, I don't know how to do it. I found some solution like this, but that solution didn't work for me:
public void openFolder(View view)
{
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
Uri uri = Uri.parse(Environment.getExternalStorageDirectory().getPath()
+"/pictures/miscursos/");
intent.setDataAndType(uri, "image/jpeg");
startActivity(Intent.createChooser(intent, "Open folder"));
}
This just opens all the images, but not the folder that I want.
The above code will only opens the folder. You have to use GallerView or GridView with the help of Adapter you can set images to ListView or GridView. You have to give correct path in-order to open the folder you want.
GridView loading photos from SD Card - refer this link

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())));

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 camera (getting imageView from a different class)

I'm currently working on a app which the user can answer a question through images by taking photos or uploading photos from the album.
I've separated into two classes which are FulfillPhotoTaskActivity, AddPhotoActivity.
FulfillPhotoTaskActivity have imageView, addphoto button and save button.
when I press addphoto button, it goes to AddPhotoActivity which we can select two options(taking photos or uploading photos). I finished the part where if the user press take photo button then it opens the camera and take the photo. I created onActivityResult which get the image data. but in many example, inside the onActivityResult they also have imageView, like setImageBitmap.
My problem is, how can I get the imageView from the AddPhotoActivity and shows it in FulfillPhotoTaskActivity which I already made for XML.
When you take a photo it saves the image to a file. You can retrieve this image file and then you can call your imageView in FulfillPhotoTaskActivity and set the imageView to the image that is in the file.
The following code will start the camera intent and the file to a given location:
File root = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder/SubFolder");
String folder = root.toString();
File file = new File(folder, "fileName" + ".jpg");
Uri outputFileUri = Uri.fromFile(file);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(cameraIntent, 0);
Now when the user takes a picture you know where it will be saved. In your FulfillPhotoTaskActivity you can call your imageview:
ImageView imageView = (ImageView)findViewById(R.id.YOUR_IMAGE_VIEW_ID);
Finally, you can just set the image to the imageview:
imageView.setImageBitmap(BitmapFactory.decodeFile(file));
So, you don't need to get the imageview from AddPhotoActivity. You only need the file name. Then, you can go back to FulfillPhotoTaskActivity and set the imageview to the bitmap file.
I hope this helps!

Categories

Resources