Reputation: 238
I am doing a project in which user can add upto 10 images. When user clicks on "Add new image" button an imageview will be created. I have implemented this in a horizontal scrolling imageview, so that user can scroll horizontally for viewing the images. On clicking on an image, user will be able to add image from camera or gallery. Now the issue is always the image is getting setted on the last imageview even though I clicked on other imageviews. I don't know how to set the image in the selected imageview.
I have referred this (http://sunil-android.blogspot.in/2013/03/insert-imageview-dynamically-using-java.html) link for dynamically creating imageview in horizontal scrollview.
Following is my code:
On clicking on the button imageviews will be added dynamically:
btn_upload.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
addImageView(image_layout);
}
});
This is the addImageView function:
private void addImageView(LinearLayout layout) {
imageView = new ImageView(this);
imageView.setImageResource(R.drawable.gallery);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(220, 220);
imageView.setLayoutParams(layoutParams);
imageView.setPadding(0, 0, 10, 0);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setId(temp);
layout.addView(imageView);
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_id = v.getId();
showDialog(CONTEXT_MENU_ID);
}
});
}
This is the code for selecting image from camera or gallery :
protected Dialog onCreateDialog(int id) {
if (id == CONTEXT_MENU_ID) {
return iconContextMenu.createMenu();
}
return super.onCreateDialog(id);
}
@SuppressWarnings("deprecation")
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK)
return;
switch (requestCode) {
case PICK_FROM_CAMERA:
doCrop();
break;
case PICK_FROM_FILE:
mImageCaptureUri = data.getData();
doCrop();
break;
case CROP_FROM_CAMERA:
Bundle extras = data.getExtras();
if (extras != null) {
SelectedImage = extras.getParcelable("data");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SelectedImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
imageData = baos.toByteArray();
image = Base64.encodeBytes(imageData);
filename = "img_" + System.currentTimeMillis();
imageView.setImageBitmap(SelectedImage); //setting the image
} else {
image = "";
}
File f = new File(mImageCaptureUri.getPath());
if (f.exists())
f.delete();
break;
}
}
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Cannot find image cropping application", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent = new Intent(intent);
co.appIntent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter(adapter,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
startActivityForResult(
cropOptions.get(item).appIntent,
CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener(new DialogInterface.OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
if (mImageCaptureUri != null) {
getContentResolver().delete(mImageCaptureUri, null, null);
mImageCaptureUri = null;
}
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
}
Upvotes: 3
Views: 1312
Reputation: 12685
take a class variable ImageView
like
ImageView addImage;
change your click method like this
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
iv_id = v.getId();
addImage = (ImageView)v // added code
showDialog(CONTEXT_MENU_ID);
}
});
and finally in onActivityResult
replace this line
imageView.setImageBitmap(SelectedImage); //setting the image
with
addImage.setImageBitmap(SelectedImage); //setting the image
hope you will understand the actul problem.
Upvotes: 1