How to display image from cursor in ImageView? - Android
I am attempting to display the images from the SD card into a Gallery and then selecting an image in the gallery displays it in a larger ImageView. I have successfully implemented the gallery (see code below) but after working with the cursor and String[] for some time I havent been able to figure out how to display the image selected in an ImageView with an OnItemClickListener...my code is below:
import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Base64;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;
public class Send extends Activity {
private Cursor cursor;
private int columnIndex;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sendlayout);
final String[] projection = {MediaStore.Images.Thumbnails._ID};
cursor = managedQuery( MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI,
projection, null, null, MediaStore.Images.Thumbnails.IMAGE_ID);
columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Thumbnails._ID);
final ImageView imageView = (ImageView) findViewById(R.id.imageView10);
imageView.setImageResource(R.raw.blackinscreen);
final Gallery sdcardImages = (Gallery) findViewById(R.id.gallery10);
sdcardImages.setAdapter(new ImageAdapter(this));
sdcardImages.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
}
});
}
private class ImageAdapter extends BaseAdapter {
private Context context;
private int itemBackground;
public ImageAdapter(Context localContext) {
context = localContext;
TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
itemBackground = a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground, 0);
a.recycle();
}
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) {
ImageV开发者_开发知识库iew picturesView;
if (convertView == null) {
picturesView = new ImageView(context);
cursor.moveToPosition(position);
int imageID = cursor.getInt(columnIndex);
picturesView.setImageURI(Uri.withAppendedPath(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, "" + imageID));
picturesView.setScaleType(ImageView.ScaleType.FIT_XY);
picturesView.setLayoutParams(new Gallery.LayoutParams(150, 120));
picturesView.setBackgroundResource(itemBackground);
}
else {
picturesView = (ImageView)convertView;
}
return picturesView;
}
}
}
The xml has a gallery at the top and an image display below it. With this code, how do I display the selected image from the gallery into the ImageView? Thank you for your time.
I recommand you to store your imageID somewhere on your Adapter
Then you can get the adapter of your Gallery on your OnItemClickListener then get the corresponding item
ImageAdapter imageAdapter = (ImageAdapter) arg0.getAdapter();
or
ImageAdapter imageAdapter = sdcardImages.getAdapter();
Then you can get your imageID previously stored (you have to adapt your getItem method) :
int imageID = imageAdapter.getItem(position);
Then you're good to change your ImageView source :)
精彩评论