从库中选择图像到imageview

时间:2013-11-25 15:59:04

标签: android xml

我在从图库中选择图像并将其设置为imageview时遇到问题。假设我有两个活动; mainActivity包含图库和secondactivity按钮,其中包含必须显示图片的imageview

 b1.setOnClickListener(new OnClickListener() {
    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);
   }
   });

请给我两个人的代码......

5 个答案:

答案 0 :(得分:19)

这是从图库中加载图片的代码:

public class ImageGalleryDemoActivity extends Activity {


    private static int RESULT_LOAD_IMAGE = 1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                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) {
        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();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

答案 1 :(得分:6)

使用Picasso

可以单行完成,您无需进行cursor query

我已将其扩展以便更好地理解: -

选择图片

    public void pickImage() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

    }

加载图片

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {

                    Uri selectedImageURI = data.getData();

                    Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
                            .into((ImageView) findViewById(R.id.imageView1));
                }

            }
        }

答案 2 :(得分:3)

就我而言,解决方案

 private void OpenGallery(){
    Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getImageIntent .setType("image/*");
    startActivityForResult(getImageIntent , IMAGE_PICKER );
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode== IMAGE_PICKER  && resultCode == RESULT_OK) {
        Uri fullPhotoUri = data.getData();
        imageView.setImageURI(fullPhotoUri);
    }
}

您可以在活动之间使用putExtra(“ fullPhotoUri”,fullPhotoUri.toString())共享URI。

答案 3 :(得分:1)

试试这个。

 b1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                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) {
        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();
            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
        }
    }

这里有一些例子

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

答案 4 :(得分:0)

要从图库中选择图片,请使用此代码。

btn_imageSetter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_PICKER);

    }
});

现在使用此方法将图像设置为ImageViewer

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
        try{
            final Uri uriImage = data.getData();
            final InputStream inputStream = getContentResolver().openInputStream(uriImage);
            final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
            iv_image.setImageBitmap(imageMap);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
        }

    }


}