
Using the following example we can display the thumbnails of images stored in the sd card. Here im displaying the thumbnails in the Grid view.
You should to add the folowing permission in the manifest xml file.
< uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Sd card should be available with the emulator or real device.
ImageThumbnailsActivity is the main Activity.
AndroidManifest.xml
--------------------
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="image.Thumbnails" android:versionCode="1" android:versionName="1.0.0">
< uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
< application android:icon="@drawable/icon" android:label="@string/app_name">
< activity android:name=".ImageThumbnailsActivity"
android:label="@string/app_name">
< intent-filter>
< action android:name="android.intent.action.MAIN" />
< category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
< activity android:name=".ViewImage">
< intent-filter>
< action android:name="android.intent.action.VIEW" />
< category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
package image.Thumbnails;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
public class ImageThumbnailsActivity extends Activity {
/** Called when the activity is first created. */
private Cursor imagecursor, actualimagecursor;
private int image_column_index, actual_image_column_index;
GridView imagegrid;
private int count;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init_phone_image_grid();
}
private void init_phone_image_grid() {
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, null,
null, MediaStore.Images.Thumbnails.IMAGE_ID + "");
image_column_index = imagecursor
.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
count = imagecursor.getCount();
imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imagegrid.setAdapter(new ImageAdapter(getApplicationContext()));
imagegrid.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView> parent, View v,
int position, long id) {
System.gc();
String[] proj = { MediaStore.Images.Media.DATA };
actualimagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj,
null, null, null);
actual_image_column_index = actualimagecursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
actualimagecursor.moveToPosition(position);
String i = actualimagecursor.getString(actual_image_column_index);
System.gc();
Intent intent = new Intent(getApplicationContext(), ViewImage.class);
intent.putExtra("filename", i);
startActivity(intent);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return count;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position,View convertView,ViewGroup parent) {
System.gc();
ImageView i = new ImageView(mContext.getApplicationContext());
if (convertView == null) {
imagecursor.moveToPosition(position);
int id = imagecursor.getInt(image_column_index);
i.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, ""
+ id));
i.setScaleType(ImageView.ScaleType.CENTER_CROP);
i.setLayoutParams(new GridView.LayoutParams(92, 92));
}
else {
i = (ImageView) convertView;
}
return i;
}
}
}
// By selecting the thumbnails user can view the actual image.
package image.Thumbnails;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ViewImage extends Activity {
private String filename;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
System.gc();
Intent i = getIntent();
Bundle extras = i.getExtras();
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inSampleSize = 2;
filename = extras.getString("filename");
ImageView iv = new ImageView(getApplicationContext());
Bitmap bm = BitmapFactory.decodeFile(filename, bfo);
iv.setImageBitmap(bm);
setContentView(iv);
}
}

//main.xml
< ?xml version="1.0" encoding="utf-8"?>
< LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
< GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/PhoneImageGrid" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:numColumns="auto_fit"
android:verticalSpacing="10dp" android:horizontalSpacing="10dp"
android:columnWidth="90dp" android:stretchMode="columnWidth"
android:gravity="center" />
< /LinearLayout>
I've completed very similar code but am struggling with getting thumbnails in my emulator SD card. I have added folders just as they appear on my phone's SD card but when I get a count on the number of thumbnails there are based on the thumbnail external content URI, the count is always zero. Can you help me understand what I'm missing here???
ReplyDeletehi,
ReplyDeleteI didnt create any folders in my SD card. But later i found two folders named as albumthumbs and dcim after copying some image files to SD card. Do you have the same? Can you Check it?
Thanks.
not as fluently as in default app Gallery....
ReplyDeletecould you find which part slows this activity down?
Thx!
the bug in this application is that *.JPG images will not be shown while *.jpg are present.
ReplyDeleteactually that wasnt the problem ...
ReplyDeletethe problem is ..if you add images and dont open the gallery before using this app..
opening the gallery makes thumbnails ... after which the images show up in the app in the right place
when i run this application images are reapeated afeter some images so any soultion ofr that issue?
ReplyDeleteJaimin, i have the same problem.
ReplyDeleteBeside the scroll function works very slow.
This comment has been removed by the author.
ReplyDeleteHi Ramesh,
ReplyDeleteThere is nothing getting displayed on the Emulator.I followed the same stuff.My sd card has got .jpeg files.Count value is always zero.Could you tell me where I am going wrong
Ramesh - go to menu, dev tools, and media scanner. seems to be a bug in the emulator. Then go back and you should she the images.
ReplyDeleteHi,
ReplyDeleteI don't understand why this is too complicated just to display a full image from a list.
I'm coming from web applications dev and I'm new on android developpement but I found that too heavy to develop basic action like displaying a big image from a small one.
Is android really matters ?
Thanks for the post! I was able to run this app. How do I get pictures from a specific folder? Do I need to change the query?
ReplyDeleteYes you can give folder path.
ReplyDeletethanks man!!! for the fabulous code. it helps me a lot
ReplyDeleteWhy do not you use this class : http://developer.android.com/reference/android/provider/MediaStore.Images.Thumbnails.html
ReplyDeleteand I think there is also this problem : "the problem is ..if you add images and dont open the gallery before using this app..". The thumbnail are only created by the gallery...
how did you store your images in the sdcard?
ReplyDeleteHello, nice tutorial but on my Nexus (2.2) I always get the following error:
ReplyDeleteW/ImageView( 4232): Unable to open content: content://media/external/images/thumbnails/1
I/System.out( 4232): resolveUri failed on bad bitmap uri: content://media/external/images/thumbnails/1
Do you have an idea what I should change?
On Windows, run DDMS perspective and you can access the simulated file system ......
ReplyDeletehi
ReplyDeletebut this code is not working for OS version 1.5 and 1.6 . do you have any other solution which is commen for both 1.5 and 2.0
Hi Dear,
ReplyDeleteI used your code but did not display any image in device what can i do,please give any solution for display image in device
Thank you very much, I looked over the web for such example, work great on my Nexus one.
ReplyDeletei cant even begin to tell you how awesome you are...
ReplyDeleteMan I am realy very thank ful..
ReplyDeleteI just copied the code and every thing worked fine.
Gr8 work man ..
Thanks once again
Hello 2All,
ReplyDeleteI am new in Android.
First of all thanks for this tutorial.
But I have one doubt:
"Can anyone suggest me that what is the location of all that pictures.
I mean to say that from where all the pictures are seen in the Greed View?"
Latest reviews and news about android phones, free droid app and android mobile
ReplyDeletefree android app
Sometimes I receive the wrong image with this code, is there any fix to this?
ReplyDeleteImageAdapter getView is completely wrong. You should update image uri every time, not just in cases where convertView is null - this will fix wrong and repeated images problem. Also these gc scares me much.
ReplyDeletehi,
ReplyDeletewhen i click the thumbnail image it does not view the next class can anybody help me i's very urgent pls
pls respond
ReplyDeleteSorry guys,
ReplyDeleteIt will work if any idea for zooming is images.pls reply for me.
thanks.
Sorry guys,
ReplyDeleteIt will work if any idea for zooming this image.pls reply for me
thanks.
Any one please tell how to Parse image from url in Gridview.I am unable to find any example
ReplyDeleteThanks man for great code.
ReplyDeleteBut can any buddy tell me how to increase there performance during scroll. Because it is not as fluent as default apps.
how can i change the size of the displayed image.
ReplyDeleteI keep running into OutOfMemoryException on my ADP2 phone, each time I scroll down the GridView.
ReplyDeleteHas anyone run into the same problem?
This comment has been removed by the author.
ReplyDeleteWhat if you just wanted to display the pics that are in your camera folder
ReplyDelete/sdcard/DMIC/Camera
This works fine for me to retrieve images from sd card. But im trying to display the clicked image in another Activity where i have an ImageView and some buttons in that layout. Please help me.
ReplyDeleteHow to display images from a folder dynamically, like pictures stored in application data directory "/data/data/com.packagename/images/".
ReplyDelete*** Get Your FREE FACEBOOK T-Shirt, Register Now http://goo.gl/MwZGv ***
ReplyDeleteHi Dave, I just copy pasted the code .But nothing is showing?why its so?
ReplyDeleteHow do you access photos in the Pictures folder on the device? How should the URI look? Like the photos taken with the device?
ReplyDeletehi i have also same problem
ReplyDeleteThanks for the article. the code worked for me.
ReplyDeleteThanks a lot, Ditto!!!
ReplyDeletehey how can i show image's name also.
ReplyDeleteI posted some code updates in the following blog entry:
ReplyDeletehttp://www.selikoff.net/2011/07/12/improving-the-custom-android-gallery/
The implementation I'm attempting allows a user to take a photo of a person or place. That photo is saved on the SD card and the image URI is stored in SQLite. Then I want them to be able to view the images later. If they open details for a specific person my app will query the database for all image URI's associated with that person and show a gallery of clickable thumbnails. I've gotten to the point of taking the pictures, but I cannot redisplay them in the gallery. Is this possible? A gallery of thumbnails from stored URIs?
ReplyDeleteHey this code worked for me... thanks for that but i wanna know how to show the photos from camera i.e. DCIM folder. means everytime we take new photos it should be visible in grid view also... plz help me for this... i will be thankfull...
ReplyDeleteThanks! it help me. I have a question: if my internet connection is too slow, how show a ProccessDialog to show a waiting message?
ReplyDeleteThanks again!!
http:\\www.tutorialandroid.com
thanks
ReplyDeletehi brindha h ru?
ReplyDeletesorry, i just read your blog. it is really useful. But, i don't know how to give a specific folder path. Can you show me?
ReplyDeleteThis comment has been removed by the author.
ReplyDeleteThis code doesnt work on honeycomb... The method is deprecated... Kindly post a relevant tutorial for Honeycomb Thanks....
ReplyDeleteHey dude..!
ReplyDeleteIt's Really working fine.. thanks for that..!
Could you please help me to download images from the URL and store in a temporary folder..?
how to give a specific folder path. Can you show me?
ReplyDeleteYou can specify your folder path as 4th parameter as shown below:
Deletecursor = managedQuery( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection,
MediaStore.Images.Media.DATA + " like ? ",
new String[] {"%sdcard/mypics%"},
null);
i do same thing but can`t working this code.i create two folder in side the sdcard as like you do.after copy some images from them but when i fetch the image id the cursor will be null value.how to slove this.plz say me the way it`s urgent.
ReplyDeleteThanks in Advance
hai please try this one. after u insert the images in the sdcard then open the emulator gallery and check the images are there.. if it is there once again run the code.. it will work.. i had also the same problem i did the same thing. try this reply what u will get..
Deletethank you very much..i was searched this for long time. i saw many blogs but i was not satisfied about their codes. today only i saw your blog it was really superb. thank you once again..... keep posting like this
ReplyDeleteHere its so many errors !!!
ReplyDeleteThis works great for loading what's in the standards gallery, however if i change the first managedQuery to point to my folder (saved in root of sdcard) then no results are returned.
ReplyDeleteI know that the location is correct as if i instead change the second managedQuery to point to my folder then when i click on a thumbnail it loads one of the images from that folder instead, proving it can see it on some level.
The images in that folder are loaded on externally rather than being taken by the phones camera so don't know if possibly these images are not in a library or have meta info or something perhaps?
String[] img = { MediaStore.Images.Thumbnails._ID };
imagecursor = managedQuery(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, img, MediaStore.Images.Thumbnails.DATA + " like ? ",new String[] {"%sdcard/myfolder%"}, null);
image_column_index = imagecursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
Could someone tell me why thumbnail isn't displayed?
ReplyDeleteand beautify your code?
ReplyDeleteeasy for others to take a look!