android - 从图库中挑选照片并在 ImageView 中显示

标签 android image android-gallery

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

一切正常,但有时,当我选择某些特定图像时,预览不显示。我也尝试过压缩图像,但仍然无法正常工作

我的代码如下.. 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));

} 

最佳答案

这样试试

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        super.onActivityResult(requestCode, resultCode, intent);

        switch (requestCode) {
                case RESULT_LOAD_IMAGE:
            if (resultCode == Activity.RESULT_OK) {

                Uri selectedImage = intent.getData();
                try {
                    Bitmap bitmapImage =decodeBitmap(selectedImage );
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
                                // Show the Selected Image on ImageView
                    ImageView imageView = (ImageView) findViewById(R.id.imgView);
                    imageView.setImageBitmap(bitmapImage);

            }

public  Bitmap decodeBitmap(Uri selectedImage) throws FileNotFoundException {
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);

        final int REQUIRED_SIZE = 100;

        int width_tmp = o.outWidth, height_tmp = o.outHeight;
        int scale = 1;
        while (true) {
            if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
                break;
            }
            width_tmp /= 2;
            height_tmp /= 2;
            scale *= 2;
        }

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
    }

关于android - 从图库中挑选照片并在 ImageView 中显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21136311/

相关文章:

android - 如何使用 Camera2 获取前置摄像头的 ID?

image - PDFBox图片大小问题

android - 如何使用 Intent.ACTION_GET_CONTENT 过滤掉不需要的文件

android - Gallery 和 Camera-taken-photo 的 URI/URL 路径不同

android - 我无法将我的位图保存到 Android 中的图库中?

android - 尝试使用 NDK 为 Android 构建 PocketSphinx 时出现问题

android - android内存泄漏背后的逻辑

Android 如何从剪贴板 onPrimaryClipChanged 获取字符串?

javascript - 如何在图像的特定点显示注释?

Swift - 图像不显示