Using this code you can fetch images from sdcard and display it in a gridview
Sdcard.java
package com.example.sdcard;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Context;
import android.database.Cursor;
import android.view.Menu;
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 Sdcard extends Activity {
//
Cursor used to access the results from
querying for images on the SD card.
private Cursor cursor;
//
Column index for the Thumbnails Image IDs.
private int columnIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sdcard);
// Set up an array of the Thumbnail Image ID column we want
String[] projection = {MediaStore.Images.Thumbnails._ID};
// Create the cursor pointing to the SDCard
cursor = managedQuery(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, // Which columns to
return
null, // Return all rows
null,
MediaStore.Images.Thumbnails.IMAGE_ID);
// Get the column index of the Thumbnails Image ID
columnIndex =
cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
GridView sdcardImages = (GridView) findViewById(R.id.gridView1);
sdcardImages.setAdapter(new ImageAdapter(this));
}
// Image adapter to link images to the gridview
private class ImageAdapter extends BaseAdapter {
private Context context;
public ImageAdapter(Context localContext) {
context = localContext;
}
public int getCount() {
return cursor.getCount();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int
position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView picturesView;
if (convertView == null) {
picturesView = new
ImageView(context);
// Move cursor to current
position
cursor.moveToPosition(position);
// Get the current value for
the requested column
int imageID =
cursor.getInt(columnIndex);
// Set the content of the image
based on the provided URI
picturesView.setImageURI(Uri.withAppendedPath(
MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" +
imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
picturesView.setPadding(10, 10,
10, 10);
picturesView.setLayoutParams(new GridView.LayoutParams(100, 100));
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
Here is the main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<GridView
android:id="@+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:columnWidth="90dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:numColumns="2" >
</GridView>
</RelativeLayout>
for Imageadapter.java class,we have to create another java class or else can we write in the existing class?
ReplyDeleteIn the existing class
ReplyDeleteThe Images are repeating twice on my xperia ray (as an emulator)
ReplyDeleteI mean, the onCreate method is called twice. and also, it does not load all images on my sdcard, any idea how? and also, how about in terms of videos? thanks!
This comment has been removed by the author.
ReplyDeleteThank You.....
ReplyDeleteimages r repeated,not a working code sorry
ReplyDeletewhat about manifest file
ReplyDeleteFinally a working solution, thanks!
ReplyDeleteThe error says its Permission error But I have Listed the permission in manifest file.
ReplyDeletedid you take permission both of them permission....
Deletewrite-external-storage and read-external-storage
Why are images repeating twice? Please Reply...
ReplyDeleteAny idea how to delete the selected gridview item onLongClick from the SD card permanently?
ReplyDeleteAny idea how to delete the selected gridview item onLongClick from the SD card permanently?
ReplyDeleteif I want to retrieve images from a specific folder from SD card?
ReplyDeleteerror was in
ReplyDeletecursor.moveToPosition(position);
int imageID = cursor.getInt(columnIndex);
This comment has been removed by the author.
ReplyDeleteFollowed the same code and i got this error..plz help me
ReplyDeleteUnable to open content: content://media/external/images/thumbnails/2
java.io.FileNotFoundException: No such file or directory
in the getView method of ImageAdapter class
IMages are repeating
ReplyDeleteif i want to get images from specific folder then where to change?
ReplyDeleteThank u...how do i retrieve image in a particular folder from sd card
ReplyDeleteThank rất nhiều
ReplyDeletePhối chó bull pháp
Phối giống chó Corgi
Phối chó Pug
Dịch vụ phối giống chó Poodle
Dịch vụ phối giống chó bull pháp
Ke na lehlohonolo haholo ho bona sehlooho sena. Ke lakatsa eka u tla lula u thabile ebile u thabile
ReplyDeletegiảo cổ lam giảm cân
giảo cổ lam giảm béo
giảo cổ lam giá bao nhiêu
giảo cổ lam ở đâu tốt nhất
Ihe 1 na-adọrọ mmasị. Daalụ maka ịkekọrịta
ReplyDeletemáy khuếch tán tinh dầu
máy khuếch tán tinh dầu giá rẻ
máy phun tinh dầu
máy khuếch tán tinh dầu tphcm
máy phun sương tinh dầu
İlginç ve ilginç bir makale.
ReplyDeletegiảo cổ lam 5 lá
giảo cổ lam 7 lá
giảo cổ lam khô
giảo cổ lam 9 lá
Hey, would you mind if I share your blog with my twitter group? There’s a lot of folks that I think would enjoy your content. Please let me know. Thank you.
ReplyDeleteJava Training in Chennai | J2EE Training in Chennai | Advanced Java Training in Chennai | Core Java Training in Chennai | Java Training institute in Chennai
Appreciation for really being thoughtful and also for deciding on certain marvelous guides most people really want to be aware of....
ReplyDeleteData science training chennai | data science course chennai
very well explained. I would like to thank you for the efforts you had made for writing this awesome article. This article inspired me to read more. keep it up.Java Training in Chennai
ReplyDeleteJava Training in Velachery
Java Training inTambaram
Java Training in Porur
Java Training in Omr
Java Training in Annanagar
This information is impressive; I am inspired with your post writing style & how continuously you describe this topic. After reading your post,
ReplyDeleteDigital Marketing Training in Velachery
Digital Marketing Training in Tambaram
Digital Marketing Training in Porur
Digital Marketing Training in Omr
Digital MarketingTraining in Annanagar
Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.
ReplyDeleteSoftware Testing Training in Chennai
Software Testing Training in Velachery
Software Testing Training in Tambaram
Software Testing Training in Porur
Software Testing Training in Omr
Software Testing Training in Annanagar