Reputation: 4446
I want any one image present in the sdcard.
I only know the image
name.
But how can i retrieve that particular image within my program(including path). And also to convert into a bitmap
or stored in a bitmap variable
How can I do that?
Thank you
Upvotes: 1
Views: 1044
Reputation: 4135
Here, you can show all images in SDCard and when you click one of them, you get the path of selected image:
try {
// 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.gallary);
sdcardImages.setAdapter(new ImageAdapter(this));
// Set up a click listener
sdcardImages.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v,
int position, long id) {
// Get the data location of the image
String[] projection = { MediaStore.Images.Media.DATA };
cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
projection, // Which columns to return
null, // Return all rows
null, null);
columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToPosition(position);
// Get image filename
String imagePath = cursor.getString(columnIndex);
// Use this path to do further processing, i.e. full screen
// display
}
});
} catch (Exception e) {
e.printStackTrace();
}
And here is the ImageAdapter:
/**
* Adapter for our image files.
*/
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_CENTER);
picturesView.setPadding(8, 8, 8, 8);
picturesView
.setLayoutParams(new GridView.LayoutParams(100, 100));
} else {
picturesView = (ImageView) convertView;
}
return picturesView;
}
}
And if you know the exact path and image name, you can get Bitmap
by using:
Bitmap bitmap = BitmapFactory.decodeFile("yourImageDirectory" + "yourImageName.png");
Upvotes: 1