如何动态地将图像添加到ImageView?

How to add Image to ImageView dynamically?

我正在做一个用户最多可以添加 10 张图片的项目。当用户点击 "Add new image" 按钮时,将创建一个图像视图。我已经在水平滚动图像视图中实现了这一点,以便用户可以水平滚动以查看图像。单击图像后,用户将能够从相机或画廊添加图像。现在问题总是图像被设置在最后一个图像视图上,即使我点击了其他图像视图。我不知道如何在所选图像视图中设置图像。

我已将此 (http://sunil-android.blogspot.in/2013/03/insert-imageview-dynamically-using-java.html) link 用于在水平滚动视图中动态创建图像视图。

以下是我的代码:

单击按钮时将动态添加图像视图:

btn_upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

                addImageView(image_layout);
        }

});

这是 addImageView 函数:

  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);  
        }
    });
}

这是从相机或图库中选择图像的代码:

 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();
        }
    }
}

取一个class变量ImageView 喜欢

ImageView addImage;

像这样更改您的点击方法

imageView.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {

        iv_id = v.getId();

        addImage = (ImageView)v  // added code 

        showDialog(CONTEXT_MENU_ID);  
    }
});

最后在 onActivityResult 中替换此行

imageView.setImageBitmap(SelectedImage); //setting the image

addImage.setImageBitmap(SelectedImage); //setting the image

希望您能理解实际问题。