【问题标题】:Picking a photo from gallery and show in a image view从图库中挑选照片并在图像视图中显示
【发布时间】:2014-02-03 20:33:14
【问题描述】:

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

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

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

} 

【问题讨论】:

    标签: android image android-gallery


    【解决方案1】:

    这样试试

    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);
        }
    

    【讨论】:

    • 您的代码运行良好。但是如何增加 ImageView 的 with 和 Height?
    【解决方案2】:

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

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

    我打赌你想从image-chooser-library尝试这个项目

    它非常易于使用,并为您解决了所有棘手的问题,例如来自 picasa 的图像等。

    希望对你有用。

    【讨论】:

    • 我已经测试过你的,但是当我从手机中选择一些图像时,它会给出“找不到文件”
    • 您是否从突出显示中选择了图像?是这样,检查一下 dev_get_content 分支,看看它是否有效。
    • 嘿,如果你认为有错误,你可以在那里创建一个问题 :) 作者会感谢你的努力。
    • 谢谢你的代码,你的代码工作得很好。我已经测试过了
    【解决方案3】:

    您尝试在onActivityResult() 中加载位图的方式并不完全正确。有时您将无法打开图像并且您的应用程序可能会崩溃。你最好使用这样的代码:

    Uri imageUri = data.getData();
    InputStream imageStream = null;
    try {
        imageStream = getContentResolver().openInputStream(imageUri);
        ImageView imageView = (ImageView) findViewById(R.id.imgView);
        imageView.setImageBitmap(BitmapFactory.decodeStream(imageStream));
    } catch (FileNotFoundException e) {
        // Handle the error
    } finally {
        if (imageStream != null) {
            try {
                imageStream.close();
            } catch (IOException e) {
                // Ignore the exception
            }
        }
    }
    

    【讨论】:

      【解决方案4】:

      在 imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 之后添加这个;

      imageView.setImageURI(selectedImage);
      

      它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多