【问题标题】:BitmapFactory.decodeFile() returns null in some devicesBitmapFactory.decodeFile() 在某些设备中返回 null
【发布时间】:2018-05-03 13:58:22
【问题描述】:

我正在使用来自here 的以下代码。我想压缩图像。

BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);

int actualHeight = options.outHeight;
int actualWidth = options.outWidth;

从相机、图库和照片中选择图像后,我会根据操作系统类型和设备型号在不同设备中获得不同类型的路径。喜欢:

1) /storage/emulated/0/Android/data/...

2) /raw//storage/emulated/0/mb/1511172993547.jpg

3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582

如果路径类似于第一个 url,则此代码工作正常。但是如果我得到其他类型的图像,那么 BitmapFactory.decodeFile() 将给出 null。

有没有办法在所有类型的设备和操作系统版本中压缩图像。

更新:

打开选择器:

Intent pickIntent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
pickIntent.setType("image/*");
startActivityForResult(pickIntent, 1001);

选择图片后:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Uri imgUri = Uri.fromFile(new File(data.getData().getPath()));
    Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);
    dlgImageToPost.setImageBitmap(cmpBitmap);
...
}

压缩:

public static Bitmap compressUriQuality(Context mContext, Uri selectedImage) {
    InputStream imageStream = null;
    Bitmap bmp = null;
    try {
        imageStream = mContext.getContentResolver().openInputStream(
                selectedImage);
        bmp = BitmapFactory.decodeStream(imageStream);
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        bmp.compress(Bitmap.CompressFormat.PNG, 50, stream);

        if (imageStream != null)
            imageStream.close();
        stream.close();
        stream = null;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return bmp;
}

【问题讨论】:

  • 你有图像Uri,所以打开它的InputStream并使用BitmapFactory解码它 - 你可以使用ContentResolver API得到InputStream
  • 你是想从厨房或任何其他路径获取图像路径???
  • 不,不要使用任何“图像路径” - 你只需要Uri
  • 感谢您的评论,我会尝试使用 Uri

标签: android bitmap bitmapfactory image-compression


【解决方案1】:

电视 2) /raw//storage/emulated/0/mb/1511172993547.jpg

如果用户从“图库”或“照片”中选择图片,这不是您将获得的路径。当然,它不是您可以用于 .decodeFile() 的文件系统路径。你是怎么获得的?

3) /2/1/content://media/external/images/media/11647/ORIGINAL/NONE/486073582

这不是有效的内容方案路径。你是如何获得它的?当然不能用于 .decodeFile()。

  i'm getting different type of paths 

如果你“表现正常”,你将永远不会得到这样的路径。那么你在做什么呢?基本 uri 处理错误?

using following code from here. 

正如您现在所看到的,这在很大程度上是相当肮脏的示例代码。

any way to compress image in all types of devices and OS versions.

当然。直接使用获取到的选中uri即可。为它打开一个 InputStream 并使用 .decodeStream() 代替。

我的上帝..你没有直接使用 uri。

 Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, imgUri);

改成

  Bitmap cmpBitmap = ImageUtils.compressUriQuality(OpenWallWeb.this, data.getData());

【讨论】:

  • 谢谢回复,我会尝试使用Url
  • 使用 Uri 也出现错误,当我从小米中的照片中选择图像时,data.getData().getPath() 会返回类似 '/-1/1/content://media/external /images/media/7741/ORIGINAL/NONE/1544403096'。在这种情况下,mContext.getContentResolver().openInputStream(selectedImageUri) 给出了 FileNotFoundException。
  • 请显示完整的代码。将代码放在您的帖子中。不在评论中。不可读。还从 logcat 发布完整的异常消息。在您的帖子中。
  • 内容方案在data.getData().toString()中。不要使用 getPath()。
  • 还显示您获取 uri 的意图。你没有回答:How did you obtain it?.
【解决方案2】:

我遇到了同样的问题,请为您的应用添加存储权限。

查看此链接了解更多信息

Storage permission error in Marshmallow

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 2016-10-17
    • 2018-04-15
    • 1970-01-01
    相关资源
    最近更新 更多