Android将图库中的图像转换为两个图像视图

时间:2014-12-01 15:06:05

标签: android android-imageview image-gallery

我搜索过,我发现如何从图库中获取图像并在图像视图中显示它。这是我的来源

public class CredoUploadFile extends Activity {

private EditText credouploadfileuserid, credouploadfileusername,
        credouploadfileusersurname;

private int RESULT_LOAD_IMAGE = 1;
private int choosebutton;
private RoundedImageView credouploadcenter1left,credouploadcenter1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_credo_upload_file);

    credouploadcenter1 = (RoundedImageView) findViewById(R.id.credouploadcenter1);

    credouploadcenter1left = (RoundedImageView) findViewById(R.id.credouploadcenter1left);
    credouploadcenter1left.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
            choosebutton=1;

        }
    });

    credouploadcenter1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
            startActivityForResult(i, RESULT_LOAD_IMAGE);
            choosebutton=2;

        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


        if(choosebutton==1)
            credouploadcenter1left.setImageBitmap(BitmapFactory
                    .decodeFile(picturePath));
         if(choosebutton==2)
            credouploadcenter1.setImageBitmap(BitmapFactory
                    .decodeFile(picturePath));
    }
}

} 我可以在图像视图中显示图库中的图像,但我有两个图像视图,当我点击第二个图像视图并选择一些图像结果是第一个图像视图。我无法显示图像只有我点击的图像视图。 我怎么能解决我的问题?如果有人知道解决方案,请帮助我

2 个答案:

答案 0 :(得分:0)

onActivityResult中,您对credouploadcenter1left使用相同的变量if ...

尝试:

if(choosebutton==1)
   credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
if(choosebutton==2)
   credouploadcenter1.setImageBitmap(BitmapFactory .decodeFile(picturePath));

答案 1 :(得分:0)

 if(choosebutton==1)
    credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));
 if(choosebutton==2)
    credouploadcenter1left.setImageBitmap(BitmapFactory.decodeFile(picturePath));

以下是错误,与您的选择无关,您将始终在credouploadcenter1left上设置图片。 当您选择第二个按钮时,您必须在centeruploadcenter1上进行设置。