如何从图库中选择图像?

时间:2014-04-16 08:20:31

标签: android

我正在制作一个项目,其中我使用两个ImageView,我想从gallery中选择两个不同的图像,我想设置两个不同的ImageView s 。我可以选择一个并设置一个imageview。但我不明白我怎么能在第二个imageview案件中做到这一点?

我使用的代码是......

ImageView iv1,iv2;

    private static int RESULT_LOAD_IMAGE = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


            setContentView(R.layout.layout2);

        iv1=(ImageView)findViewById(R.id.imageView21);
        iv1.setOnClickListener(this);
        iv2=(ImageView)findViewById(R.id.imageView21);
        iv2.setOnClickListener(this);

    }

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub

        if(v.getId()==R.id.imageView21){
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE);
        }
            }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        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();


            iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }

更新1 ...

它给出了问题,即使我想为一个人提供图像视图上的设置图像......

public class LayoutDisplay2 extends Activity{

    ImageView iv1, iv2;
    private static int RESULT_LOAD_IMAGE1 = 1;
    private static int RESULT_LOAD_IMAGE2 = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);


        setContentView(R.layout.layout2);

        iv1 = (ImageView) findViewById(R.id.imageView21);
        iv1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE1);
            }
        });
        iv2 = (ImageView) findViewById(R.id.imageView22);
        iv2.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent in = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(in, RESULT_LOAD_IMAGE2);
            }
        });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RESULT_LOAD_IMAGE1 && 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();

            iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }
        if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
                && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };
            Toast.makeText(getApplicationContext(), "in second",
                    Toast.LENGTH_SHORT).show();
            Log.i("Second", "in second");
            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }

    }

}

3 个答案:

答案 0 :(得分:2)

您的代码中存在一些错误。

  1. 您已为两个图片视图分配了相同的ID。

    iv1=(ImageView)findViewById(R.id.imageView21);
    
    iv2=(ImageView)findViewById(R.id.imageView21);
    
  2. 将其更改为:

        iv1=(ImageView)findViewById(R.id.imageView21);
    
        iv2=(ImageView)findViewById(R.id.imageView22);
    
    1. 进行如下更改

      if(v.getId()==R.id.imageView21){
          Intent i = new Intent(
                  Intent.ACTION_PICK,
                  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      
          startActivityForResult(i, RESULT_LOAD_IMAGE_1);
      }   
      else if(v.getId()==R.id.imageView22){
          Intent i = new Intent(
                  Intent.ACTION_PICK,
                  android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      
          startActivityForResult(i, RESULT_LOAD_IMAGE_2);
      }
      

答案 1 :(得分:2)

尝试如下:

ImageView iv1, iv2;
private static int RESULT_LOAD_IMAGE1 = 1;
private static int RESULT_LOAD_IMAGE2 = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);

    setContentView(R.layout.layout2);

    iv1 = (ImageView) findViewById(R.id.imageView21);
    iv1.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent i = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(i, RESULT_LOAD_IMAGE1);
        }
    });
    iv2 = (ImageView) findViewById(R.id.imageView22);
    iv2.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Intent in = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(in, RESULT_LOAD_IMAGE2);
        }
    });


}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE1 && 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();

        iv1.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }
    if (requestCode == RESULT_LOAD_IMAGE2 && resultCode == RESULT_OK
            && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };
        Toast.makeText(getApplicationContext(), "in second",
                Toast.LENGTH_SHORT).show();
        Log.i("Second", "in second");
        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();

        iv2.setImageBitmap(BitmapFactory.decodeFile(picturePath));

    }

}

答案 2 :(得分:0)

试试这个image-chooser-library-1.2.9.jar

简单易行

imageChooserManager = new ImageChooserManager(this, ChooserType.REQUEST_PICK_PICTURE);
imageChooserManager.setImageChooserListener(this);
imageChooserManager.choose();

按照此链接,您可以得到答案。

https://github.com/coomar2841/image-chooser-library

http://techdroid.kbeanie.com/2013/03/easy-image-chooser-library-for-android.html