-1

When I browse an image from the gallery, I select one image but in both ImageViews the same image is set.
I want a different image in my ImageViews.

Here is my screenshot:

screenshot

here is my Activity code:

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

    }
    }
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

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

Make sure constants have different values

Prashanth Debbadwar
  • 1,047
  • 18
  • 33
0
Uri selectedImage = data.getData();
imageUpload.setImageURI(selectedImage);

do not directly assign uri to image view

Rather than

get filepath from Uri and generate a bitmap and finally assign bitmap to imageview

it will works

Happy codding!!!

Mohit Trivedi
  • 699
  • 3
  • 13