当我们使用uri添加mediastore.EXTRA_OUTPUT时,我知道intent数据为null。仅在返回图像的缩略图时它不为空。但是我有3个imageViews,用户可以点击它们点击图片。单击的图片必须显示在该特定的imageView上。
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.iv_rx1:
Toast.makeText(mContext, "Clicked", Toast.LENGTH_LONG).show();
break;
case R.id.iv_rx2:
File f2 = getOutputMediaFile();
Uri uri2 = Uri.fromFile(f2);
selectImage(uri2);
Toast.makeText(mContext, "Clicked", Toast.LENGTH_LONG).show();
break;
case R.id.iv_rx3:
File f3 = getOutputMediaFile();
Uri uri3 = Uri.fromFile(f3);
selectImage(uri3);
Toast.makeText(mContext, "Clicked", Toast.LENGTH_LONG).show();
break;
case R.id.iv_rx4:
File f4 = getOutputMediaFile();
Uri uri4 = Uri.fromFile(f4);
selectImage(uri4);
Toast.makeText(mContext, "Clicked", Toast.LENGTH_LONG).show();
break;
}
}
private void selectImage(Uri uri) {
final CharSequence[] items = {"Take Photo", "Choose from Library",
"Cancel"};
final Uri uri1=uri;
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")) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, uri1);
intent.putExtra(PRESCRIPTION_URI_INTENT, uri1.getPath());
Log.d("URI VALUE", uri1.getPath());
startActivityForResult(intent, Constants.REQUEST_IMAGE_CAPTURE);
}
The on Activity Result for the same
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
switch (requestCode) {
case Constants.REQUEST_IMAGE_CAPTURE:
//String locationImage= intent.getStringExtra(PRESCRIPTION_URI_INTENT);
Bitmap bitmap = BitmapFactory.decodeFile(imageLocation);
imageView.setBitmap(bitmap);
//Want to change the image view to which bitmap must be added based on which image view the user clicks on
break;
case Constants.REQUEST_GALLERY:
Uri selectedImageUri = data.getData();
Intent galleryIntent = new Intent(mContext, newConfirmPrescriptionAct.class);
galleryIntent.putExtra(IMAGE_PATH_AS_STRING, selectedImageUri.toString());
galleryIntent.putExtra(Constants.IMAGE_SOURCE_TYPE, Constants.REQUEST_GALLERY);
galleryIntent.putExtra(DOES_CONTAIN_ONLY_OTC, false);
startActivity(galleryIntent);
break;
}//Close switch statement
} else {
Log.d("Error ", "user canceled !");
}
} //Continues for some other options (not to do with the camera)
其中String imageLocation = uri1.getPath()
包含保存图像的路径。
我可以点击图片并将其添加到一个图片视图,但不知道如何修改它以处理不同图像视图上的多次点击。我想到的一个解决方案是更改request code
并添加有关使用哪个视图的信息并添加相关的switch语句。有没有其他方法可以这样做?