我的代码包含一个网格视图,其中包含多个图像作为单独的视图。
这些图像可以由相机拍摄,也可以由用户从图库中选择
每个视图都有一个动作,允许用户向其添加图像或从适配器中完全删除视图
为了让onActivityResult()
与GridViewAdapter
结合使用,我在我的活动中定义了一个私有类,允许我从适配器中显示图像选择器对话框,以及要在主要活动中定义的onActivityResult()
。
以下是我的整个代码
public class ImageGridView extends Activity {
GridView gridView;
GridViewAdapter gridViewAdapter;
ArrayList<GridItem> list;
Uri mUri;
String imageLocation;
ImageView imageView;
public static final int IMAGE_CAPTURE =1337, IMAGE_GALLERY=12;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_grid_view);
gridView = (GridView) findViewById(R.id.gridview);
//This image view is used as a test to make sure a bitmap is being returned from onActivityResult
imageView=(ImageView)findViewById(R.id.image_original);
list = new ArrayList<>();
GridItem gridItem = new GridItem();
list.add(gridItem);
gridViewAdapter = new GridViewAdapter(ImageGridView.this, R.layout.row_gridview, list);
gridView.setAdapter(gridViewAdapter);
}
private class GridViewAdapter extends ArrayAdapter<GridItem> {
private Context mContext;
private int layoutResourceId;
private ArrayList<GridItem> mGridData = new ArrayList<GridItem>();
public GridViewAdapter(Context mContext, int layoutResourceId, ArrayList<GridItem> mGridData) {
super(mContext, layoutResourceId, mGridData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mGridData = mGridData;
}
public void setGridData(ArrayList<GridItem> mGridData) {
this.mGridData = mGridData;
notifyDataSetChanged();
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
View row = convertView;
final ViewHolder holder;
if (row == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new ViewHolder();
holder.btnAdd = (ImageView) row.findViewById(R.id.imgAdd);
holder.btnRemove = (ImageView) row.findViewById(R.id.imgRemove);
holder.imageView=(ImageView)row.findViewById(R.id.imgPrescriptionPicasso);
row.setTag(holder);
} else {
holder = (ViewHolder) row.getTag();
}
// NOT sure if this is the correct way to go about it.
// How do I add Uri of image from onActivityResult to this
if(mGridData[position].getImageLocationAsString()!=null) {
String imageLocation = mGridData[position].getImageLocationAsString();
holder.imageView.setImageBitmap(BitmapFactory.decodeFile(imageLocation);
holder.btnAdd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
selectImage();
}
});
holder.btnRemove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mGridData.remove(position);
notifyDataSetChanged();
//Remove this view entirely and notify adapter of the change
}
});
return row;
}
class ViewHolder {
ImageView imageView;
ImageView btnAdd, btnRemove;
//The buttons have been defined as image Views for now.
}
}
private void selectImage() {
final CharSequence[] items = {"Take Photo", "Choose from Gallery",
"Cancel"};
AlertDialog.Builder builder = new AlertDialog.Builder(ImageGridView.this);
builder.setTitle("Add picture of Prescription");
builder.setItems(items, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
if (items[item].equals("Take Photo")) {
File f = getOutputMediaFile();
mUri = Uri.fromFile(f);
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);
startActivityForResult(intent, IMAGE_CAPTURE);
Log.d("Clicked", "Take photo");
} else if (items[item].equals("Choose from Gallery")) {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
IMAGE_GALLERY);
}
}
});
builder.show();
}
private File getOutputMediaFile() {
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DCIM), "Medit");
if (!mediaStorageDir.exists()) {
mediaStorageDir.mkdirs();
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File mediaFile;
mediaFile = new File(mediaStorageDir.getPath() + File.separator +
"IMG" + timeStamp + ".jpg");
Log.d("reached a", mediaFile.toString());
return mediaFile;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode== Activity.RESULT_OK){
switch(requestCode) {
case IMAGE_CAPTURE:
Bitmap bitmap= BitmapFactory.decodeFile(mUri.getPath());
imageView.setImageBitmap(bitmap);
//If user loads an image, a new view is created with a blank image
GridItem gridItem = new GridItem();
list.add(gridItem);
gridViewAdapter.notifyDataSetChanged();
break;
case IMAGE_GALLERY:
Uri selectedImageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
imageLocation = cursor.getString(columnIndex);
cursor.close();
Bitmap bitmap1= BitmapFactory.decodeFile(imageLocation);
imageView.setImageBitmap(bitmap1);
//If user loads an image, a new view is created with a blank image
GridItem gridItem1 = new GridItem();
list.add(gridItem1);
gridViewAdapter.notifyDataSetChanged();
break;
}//Close switch statement
}else {
Log.d("Error ", "user canceled !");
}
}
}
我的imageView
中定义的Activity
正在onActivityResult()
中设置,只是为了测试是否实际返回了位图。
我需要帮助
view holder
中,如果用户设置了一个位图,则将位图设置为图像视图onActivityResult()
代码本身设置特定网格项的图像。 GridItem当前有一个我正在尝试使用的String,用于存储图像的图像位置(如果存在)并在getView()
GridViewAdapter
的{{1}}持有者函数中加载位图但是我被困在这里。
这样做的最终目的是让用户向GridViewAdapter
添加尽可能多的图像,最后通过意图将Uri.getPath()
(imageLocation as String)发送到我将要使用的下一个活动一个数组。
非常感谢我能为此获得的任何帮助/指导。感谢