捕获图像并动态添加到网格视图
Posted
技术标签:
【中文标题】捕获图像并动态添加到网格视图【英文标题】:Capture image and add to Grid View dynamically 【发布时间】:2016-03-29 02:05:13 【问题描述】:我有一个包含网格视图的活动。在这个网格视图中,我添加了从相机捕获/从图库中获取的图像。
目前,我在 Activity 中有一个按钮,它提供了一个选择器对话框,提示用户捕获新图像或从图库中选择一个。在 onActivityResult() 中,我将这个位图添加到位图的 ArrayList 中。每次执行此操作时,都会在网格视图中添加一个新网格。
网格视图中的x个对象->用户单击按钮捕获(在活动中)->捕获位图->添加到网格视图适配器的新对象(x + 1)->捕获的图像显示在.
此代码按原样运行良好,但现在我想删除活动中的这个“按钮”并将其移动,以便当用户点击每个项目时,用户可以捕获显示在该网格上的图像。
活动
GridItem gridItem=new GridItem();
list = new ArrayList<>();
list.add(gridItem); //Currently grid view has only one view
gridViewAdapter= new GridViewAdapter(ImageGridView.this, R.layout.row_gridview, list);
gridView.setAdapter(gridViewAdapter);
网格视图适配器
public 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;
@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 = (Button) row.findViewById(R.id.imgAdd);
holder.btnRemove =(Button) row.findViewById(R.id.imgRemove);
holder.imageView= (ImageView)row.findViewById(R.id.img);
row.setTag(holder);
else
holder = (ViewHolder) row.getTag();
holder.btnAdd.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
//Capture an image, Display bitmap in the image view
//Display new view as a call to action for the user to add another image
// Disable this button so user cannot add another image to same view
GridItem gridItem=new GridItem();
mGridData.add(gridItem);
notifyDataSetChanged();
);
holder.btnRemove.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
mGridData.remove(position);
notifyDataSetChanged();
);
return row;
static class ViewHolder
TextView titleTextView;
ImageView imageView;
Button btnAdd, btnRemove;
在活动中,我正在使用一个打开此对话框的按钮从活动中选择图像。一旦创建了位图,它就会被添加到GridItem object
,然后添加到适配器并调用notifyDataSetChanged()
。我想从本质上改变这种方法,以便用户单击每个元素/视图中的单个按钮(btnAdd)以动态添加图像。
private void selectImage()
final CharSequence[] items = "Take Photo", "Choose from Gallery";
AlertDialog.Builder builder = new AlertDialog.Builder(mContext);
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, Constants.REQUEST_IMAGE_CAPTURE);
else
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(
Intent.createChooser(intent, "Select File"),
Constants.REQUEST_GALLERY);
);
builder.show();
我已尝试在我的网格视图的适配器内添加图像选择器对话框,但我无法创建 onActivityResult()
函数,因此我不知道如何处理选择结果。
【问题讨论】:
【参考方案1】:这里的代码将帮助您从设备中选择任何文件,然后您可以根据需要在 onActivityResult 中执行操作。
public void browseClick()
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try
startActivityForResult(Intent.createChooser(intent, "Select a File to Upload"),FILE_SELECT_CODE);
catch (Exception ex)
System.out.println("browseClick :"+ex);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == FILE_SELECT_CODE)
if (resultCode == RESULT_OK)
try
String newName = "", filename = "";
int filesize = 0;
Uri uri = data.getData();
if (filesize >= FILE_SIZE_LIMIT)
Toast.makeText(
this,
"The selected file is too large. Selet a new file with size less than 2mb",
Toast.LENGTH_LONG).show();
else
String mimeType = getContentResolver().getType(uri);
if (mimeType == null)
String path = getPath(this, uri);
if (path == null)
filename = FilenameUtils
.getName(uri.toString());
else
File file = new File(path);
filename = file.getName();
else
Uri returnUri = data.getData();
Cursor returnCursor = getContentResolver().query(
returnUri, null, null, null, null);
int nameIndex = returnCursor
.getColumnIndex(OpenableColumns.DISPLAY_NAME);
int sizeIndex = returnCursor
.getColumnIndex(OpenableColumns.SIZE);
returnCursor.moveToFirst();
filename = returnCursor.getString(nameIndex);
String size = Long.toString(returnCursor
.getLong(sizeIndex));
catch (Exception e)
e.printStackTrace();
您可以使用“intent.setType()”方法设置要选择的文件类型。
【讨论】:
以上是关于捕获图像并动态添加到网格视图的主要内容,如果未能解决你的问题,请参考以下文章