Android:将图片放入选定的ImageView

时间:2017-06-13 07:02:39

标签: android android-intent imageview

这个问题只涉及一个设计疑问。我会感谢一些帮助。

基本上我有6个不同的ImageView,它们都连接到名为onImageViewClicked的相同方法,此方法将检查点击了6个ImageView中的哪一个,并启动Gallery intent到从画廊中选择一张照片。

现在我的问题是.....画廊意图的结果来自onActivityResult方法但是从那里,如何将照片分配给选定的ImageView ??? ......这里代码:

选择ImageView并启动图库的onImageViewClicked方法:

private void onImageViewClicked(View v){

        switch (v.getId()){
            case R.id.imgvW_1:
                //something here
                break;
            case R.id.imgvW_2:
                //something here
                break;
            case R.id.imgvW_3:
                //something here
                break;
            case R.id.imgvW_4:
                //something here
                break;
            case R.id.imgvW_5:
                //something here
                break;
            case R.id.imgvW_6:
                //something here
                break;

        }

        Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        startActivityForResult(gallery, PICK_IMAGE);
    }

方法onActivityResult从Intent获取结果:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            //how to assign here the image to the clickedImageView?????????
        }
    }

----编辑---- 谢谢大家的答案。来自Ramees,Hardik Maru,Mohammed Atif和Luca Rossi的所有4个答案对我来说都很好....所以其中任何一个都比其他人更方便吗?

5 个答案:

答案 0 :(得分:1)

一个简单的选择是为每个图像使用不同的请求代码,例如

  1. R.id.imgvW_1 - > PICK_IMAGE_1 = 100
  2. R.id.imgvW_2 - > PICK_IMAGE_2 = 101
  3. ECC
  4. 然后在onActivityResult上查看

    编辑:

    通过将视图ID用作请求代码

    ,Yout可以更快地完成
    private void onImageViewClicked(View v){
    
            switch (v.getId()){
                case R.id.imgvW_1:
                case R.id.imgvW_2:
                case R.id.imgvW_3:
                case R.id.imgvW_4:
                case R.id.imgvW_5:
                case R.id.imgvW_6:
            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, v.getId());
                    break;
    
            }
        }
    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK){
                imageUri = data.getData();
                switch (requestCode){
                    case R.id.imgvW_1:
                    case R.id.imgvW_2:
                    case R.id.imgvW_3:
                    case R.id.imgvW_4:
                    case R.id.imgvW_5:
                    case R.id.imgvW_6:
                     load image to request code imageview
                    break;
            }
        }
    

答案 1 :(得分:1)

您可以使用Picasso lib将图像加载到ImageView中。

以下是可用于加载图片的代码行:

Picasso.with(context).load(imageUri/path).into(R.id.imageView);

以下是毕加索的链接:https://github.com/square/picasso

以下是跟踪将用于显示图像的imageView的代码:

long idToUse;

private void onImageViewClicked(View v){

    switch (v.getId()){
        case R.id.imgvW_1:
            idToUse = R.id.imgvW_1;
            break;
        case R.id.imgvW_2:
            idToUse = R.id.imgvW_2;
            break;
        case R.id.imgvW_3:
            idToUse = R.id.imgvW_3;
            break;
        case R.id.imgvW_4:
            idToUse = R.id.imgvW_4;
            break;
        case R.id.imgvW_5:
            idToUse = R.id.imgvW_5;
            break;
        case R.id.imgvW_6:
            idToUse = R.id.imgvW_6;
            break;

    }

    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

然后你可以在Picasso语句中使用idToUse作为id。像这样:

Picasso.with(context).load(imageUri/path).into(idToUse);

答案 2 :(得分:1)

在代码顶部初始化一个ImageView,如

ImageView temp_image;

你的onImageViewClicked将成为

 case R.id.imgvW_1:
            temp_image = imgvW_1
            break;
case R.id.imgvW_2:
            temp_image = imgvW_2
            break;

最后在onActivityResult方法中使用下面的代码

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();

       temp_image.setbackground(...);// or setbitmap based on what type you have
    }
}

答案 3 :(得分:1)

首先创建一个成员变量

private ImageView clickedImage;

然后在你的clicklistener中

private void onImageViewClicked(View v){
    //assuming that this method will be called only from ImageView
    this.clickedImage = (ImageView) v;
    Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
    startActivityForResult(gallery, PICK_IMAGE);
}

然后在您的活动结果中

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
        imageUri = data.getData();
        //use Glide to pass the uri and load image in clickedImage or
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
        if(clickedImage != null){
             clickedImage.setBitmapDrawable(bitmap);
        }
    }
}

答案 4 :(得分:1)

通过在onImageViewClicked方法中设置标志来尝试它。使用以下代码

private String flag = "";    

private void onImageViewClicked(View v){

            switch (v.getId()){
                case R.id.imgvW_1:
                    flag="image1"  
                    //something here
                    break;
                case R.id.imgvW_2:
                     flag="image2"
                    //something here
                    break;
                case R.id.imgvW_3:
                    flag="image3"
                    //something here
                    break;
                case R.id.imgvW_4:
                    flag="image4"
                    //something here
                    break;
                case R.id.imgvW_5:
                     flag="image5"
                    //something here
                    break;
                case R.id.imgvW_6:
                    flag="image6"
                    //something here
                    break;

            }

            Intent gallery = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
            startActivityForResult(gallery, PICK_IMAGE);
        }

<强> onActivityResult

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK && requestCode == PICK_IMAGE){
            imageUri = data.getData();
            if(flag.equalsIgnoreCase("image1")){
               Picasso.with(context).load(imageUri).into(R.id.imageView);
            }
        }
    }