我有一个包含网格视图的活动。在此网格视图中,我添加从相机捕获的图像/从图库中获取的图像。
目前,我在活动中有一个按钮,提供一个选择器对话框,提示用户捕获新图像或从图库中选择一个。在onActivityResult()中,我将这个位图添加到Bitmap的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()
功能,因此我不知道如何处理选择的结果。< / p>
答案 0 :(得分:0)
以下代码可帮助您从设备中选择任何文件,然后您可以根据需要在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()”方法设置要选择的文件类型。