【问题标题】:When I browse an image from the gallery, I select one image but in both ImageViews the same image is set当我从图库中浏览图像时,我选择了一个图像,但在两个 ImageView 中设置了相同的图像
【发布时间】:2016-10-01 11:47:06
【问题描述】:

当我从图库中浏览图片时,我选择了一张图片,但在两个 ImageView 中设置了相同的图片。
我想在我的 ImageViews 中使用不同的图像。

这是我的截图:

这是我的活动代码:

 package com.example.deepak.imageuploaddownload;

        import android.content.Intent;
        import android.net.Uri;
        import android.provider.MediaStore;
        import android.support.v7.app.AppCompatActivity;
        import android.os.Bundle;
        import android.view.View;
        import android.widget.Button;
        import android.widget.EditText;
        import android.widget.ImageView;
        import android.widget.Switch;

        import static com.example.deepak.imageuploaddownload.R.id.imageToUpload;
        import static com.example.deepak.imageuploaddownload.R.id.imageToUploadTwo;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private static final int RESULT_LOAD_IMAGE =1;
    private static final int RESULT_LOAD_IMAGETWO =1;

    ImageView imageUpload, imageUploadTwo,imageDownlod;
    Button btnUplod, btnUplodTwo,btnDownload;
    EditText etUploadname ,etUploadnameTwo,etDownloadname;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageUpload =(ImageView)findViewById(imageToUpload);
        imageUploadTwo =(ImageView)findViewById(imageToUploadTwo);
        imageDownlod =(ImageView)findViewById(R.id.imageToDownLoad);

        btnUplod=(Button)findViewById(R.id.btnUploadImage);
        btnUplodTwo=(Button)findViewById(R.id.btnUploadImageTwo);
        btnDownload=(Button)findViewById(R.id.btnDownloadImage);

        etUploadname=(EditText)findViewById(R.id.etUploadName);
        etUploadnameTwo=(EditText)findViewById(R.id.etUploadNameTwo);
        etDownloadname=(EditText)findViewById(R.id.etDownloadName);

        imageUpload.setOnClickListener(this);
        imageUploadTwo.setOnClickListener(this);
        btnUplod.setOnClickListener(this);
        btnUplodTwo.setOnClickListener(this);
        btnDownload.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {

        switch(v.getId()){

            case R.id.imageToUpload:
                Intent gallryIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallryIntent,RESULT_LOAD_IMAGE);
                break;
            case R.id.imageToUploadTwo:
                Intent gallryIntentT = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                startActivityForResult(gallryIntentT,RESULT_LOAD_IMAGETWO);
                break;
            case  R.id.btnUploadImage:
                break;
            case R.id.btnDownloadImage:
                break;

            default:
                break;
        }}

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && data != null) {
            Uri selectedImage = data.getData();
            imageUpload.setImageURI(selectedImage);
        }

        if (requestCode == RESULT_LOAD_IMAGETWO && resultCode == RESULT_OK && data != null) {
            Uri selectedImagee = data.getData();
            imageUploadTwo.setImageURI(selectedImagee);
        }

    }
    }

【问题讨论】:

    标签: android android-imageview android-gallery


    【解决方案1】:
    private static final int RESULT_LOAD_IMAGE =1;
    private static final int RESULT_LOAD_IMAGETWO =1;
    

    确保常量具有不同的值

    【讨论】:

      【解决方案2】:
      Uri selectedImage = data.getData();
      imageUpload.setImageURI(selectedImage);
      

      不要直接将uri分配给图像视图

      而不是

      Uri获取filepath并生成bitmap,最后将bitmap分配给imageview

      会有用的

      编码愉快!!!

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-07-10
        • 2021-02-03
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多