我正在开发一个图库应用,其中我必须将图库中的所有图像显示到gridview
。我已经成功完成了,但我也希望在gridView
的第一个网格项中显示相机预览。任何好友都可以帮我做这个吗?
我的代码如下:
/**
* Created by netclues on 4/26/16.
*/
public class GalleryActivity extends Activity {
private int count;
private Bitmap[] thumbnails;
private String[] arrPath;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
final String[] columns = { MediaStore.Images.Media.DATA,
MediaStore.Images.Media._ID };
final String orderBy = MediaStore.Images.Media._ID;
Cursor imagecursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,
null, orderBy);
int image_column_index = imagecursor
.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.thumbnails = new Bitmap[this.count];
this.arrPath = new String[this.count];
Bitmap icon = BitmapFactory.decodeResource(getResources(), R.drawable.ic_camera);
thumbnails[0] =icon;
arrPath[0] = "image1";
for (int i = 1; i < this.count; i++) {
imagecursor.moveToPosition(i);
int id = imagecursor.getInt(image_column_index);
int dataColumnIndex = imagecursor
.getColumnIndex(MediaStore.Images.Media.DATA);
thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
arrPath[i] = imagecursor.getString(dataColumnIndex);
}
GridView imagegrid = (GridView) findViewById(R.id.PhoneImageGrid);
imageAdapter = new ImageAdapter();
imagegrid.setAdapter(imageAdapter);
imagecursor.close();
}
public class ImageAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ImageAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
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) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.galleryitem, null);
holder.imageview = (ImageView) convertView
.findViewById(R.id.thumbImage);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageview.setId(position);
// holder.imageview.setOnClickListener(new View.OnClickListener() {
//
// public void onClick(View v) {
// int id;
// id = v.getId();
// if (id==0) {
// System.out.print("Idpath" + id);
// }else {
// Intent intent = new Intent(GalleryActivity.this,
// lastscreen.class);
// intent.setDataAndType(Uri.parse("file://" + arrPath[id]),
// "image/*");
// intent.putExtra("path", arrPath[id]);
//
// startActivity(intent);
// }
// }
// });
holder.imageview.setImageBitmap(thumbnails[position]);
return convertView;
}
}
class ViewHolder {
ImageView imageview;
}
@Override
public void onBackPressed() {
}
}