从图库中选择一张照片并在图像视图中显示 [英] Picking a photo from gallery and show in a image view

查看:40
本文介绍了从图库中选择一张照片并在图像视图中显示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序,它有一个按钮可以从您的图库中选择一张照片,它工作正常,在选择图像后,我的应用程序显示返回活动并在图像视图中显示图像.

I have an app, which has a button to select a photo from your gallery and it works fine and after selecting the image my app show came back to the activity and shows the image in an image View.

一切正常,但有时,当我选择某些特定图像时,预览未显示.我也试过压缩图片还是不行

Every is working fine but sometimes ,when i select some particular images the preview is not showing. I have also tried to compress the image still its not working

我的代码在下面..onCreate()

My code is below.. In onCreate()

galeryBtn=(Button)findViewById(R.id.buttonGallery);
galeryBtn.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);

    }
});

在 onActivityResult(int requestCode, int resultCode, Intent 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();
    // String picturePath contains the path of selected Image

    // Show the Selected Image on ImageView
    ImageView imageView = (ImageView) findViewById(R.id.imgView);
    imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

} 

推荐答案

我遇到了类似的问题,比如从资源中获取光标 uri、打开流、设置位图等.而且它一直有错误.

I run into similar problems like getting cursor uri from resource, open stream, set bitmap etc. And it has bugs all the time.

所以我搜索了库并找到了 image-chooser-library 库.

So I searched libraries and found image-chooser-library library.

我敢打赌你想从 image-chooser-library

它非常易于使用并为您解决了所有这些基本问题,例如来自 picasa 的图像等.

It is very easy to use and solves all those nitty gritty problems for you, like images from picasa etc.

希望对你有用.

这篇关于从图库中选择一张照片并在图像视图中显示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆