2016-10-01 24 views
-1

當我從圖庫中瀏覽圖像時,我選擇了一個圖像,但在兩個ImageView中都設置了相同的圖像。
我想在我的ImageViews中使用不同的圖像。當我從圖庫中瀏覽圖像時,我選擇了一個圖像,但在兩個ImageView中設置了相同的圖像

這裏是我的截圖:

screenshot

這裏是我的活動代碼:

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

    } 
    } 

回答

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

確保常數有不同的價值觀

0
Uri selectedImage = data.getData(); 
imageUpload.setImageURI(selectedImage); 

不直接分配的URI圖像視圖

不是

烏里得到文件路徑併產生最後分配位圖的ImageView

它會工作

快樂編碼!

相關問題