【问题标题】:Android Load Camera image as BitmapAndroid将相机图像加载为位图
【发布时间】:2010-06-01 04:20:37
【问题描述】:

我正在使用 BitmapFactory.decodeFile 将图像位图加载到我的应用程序中。但是,该函数在大图像(例如来自相机的图像)上返回 null。文件路径绝对正确,我只是不知道为什么它会返回 null。我尝试了超级采样,但似乎没有帮助。

有谁知道它为什么会这样做,或者我如何可以更轻松地将相机拍摄的图像加载到位图中?

这是我正在使用的代码:

public static Bitmap loadBitmap(String filePath){
    Bitmap result = BitmapFactory.decodeFile(filePath);

    if(result == null){
        if(filePath.contains(".jpg") || filePath.contains(".png")){
            //This is the error that occurs when I attempt to load an image from the Camera DCIM folder or a large png I imported from my computer.
            Utils.Toast("Could not load file -- too big?"); 
        } else {
            Utils.Toast("Could not load file -- image file type is not supported");
        }
    }
    return result;
}

【问题讨论】:

  • 您是否将相机图像保存在 SDCard 上?路径中的所有目录都存在吗?
  • 是的,图像是通过文件浏览器选择的,因此路径肯定是正确的。在同一文件夹中选择的其他(较小的)图像可以正常工作。
  • 你能提供一些你正在使用的代码吗?
  • 添加了我使用的代码。路径是正确的(我正在使用文件管理器来选择它),只是由于某种原因它返回 null。

标签: java android bitmap


【解决方案1】:

您需要提供有关您的问题的更多信息,例如您正在使用的代码的 sn-p。如果你想知道BitmapFactory.decodeFile方法何时/为什么会返回null,你可以直接阅读它的源代码:http://casidiablo.in/BitmapFactory

例如,导致BitmapFactory.decodeFile 返回 null 的原因之一是打开文件时出现问题。奇怪的是,开发人员没有记录任何有这种问题的东西……看看评论“什么都不做。如果打开时发生异常,bm 将为空。”

public static Bitmap decodeFile(String pathName, Options opts) {
    Bitmap bm = null;
    InputStream stream = null;
    try {
        stream = new FileInputStream(pathName);
        bm = decodeStream(stream, null, opts);
    } catch (Exception e) {
        /*  do nothing.
            If the exception happened on open, bm will be null.
        */
    } finally {
        if (stream != null) {
            try {
                stream.close();
            } catch (IOException e) {
                // do nothing here
            }
        }
    }
    return bm;
}

如您所见,BitmapFactory.decodeFile 不能独立工作...但它使用BitmapFactory 类的一些其他方法(例如,BitmapFactory.decodeStreamBitmapFactory.nativeDecodeStreamBitmapFactory.finishDecode 等) .问题可能出在其中一种方法上,所以如果我是你,我会尝试阅读并理解它们的工作原理,以便知道它们在哪些情况下返回 null。

【讨论】:

  • 我刚刚发布了代码(可能在您输入回复时)。我会像你建议的那样研究一些方法。谢谢!
  • 好吧,我只是尝试了 FileInputStream 方法,它确实说文件不存在。考虑到那里有一个文件,这很奇怪。我会继续摆弄。
  • 好吧,看来是文件名有问题。当我将图像重命名为“image.jpg”而不是基于照片时间戳的名称时,我实际上可以加载图像。谢谢您的帮助。当然,我现在遇到了 OOM 异常,但这是要处理的其他事情。您是否碰巧知道我在哪里可以找到 Gallery 应用程序的来源(或任何相关的来源)。我只需要弄清楚它们在加载和显示大图像时如何解决小堆大小问题。
【解决方案2】:

也许 inSampleSize 选项可以帮助您? Strange out of memory issue while loading an image to a Bitmap object

【讨论】:

  • 我没有遇到内存不足错误,而且我尝试使用 superSampling 时似乎没有帮助。
【解决方案3】:

这听起来很明显,但请检查您的 filePath 是否确实指向一个文件。您提到您正在使用文件管理器来选择要打开的图像 - 文件管理器可能正在返回内容提供者的路径而不是文件。

使用 ContentResolver 类打开文件有一种更强大的方法,它可以打开一个到内容提供者、文件或资源的 InputStream,而无需事先知道传递的路径类型。

唯一的问题是您需要在调用 openInputStream() 时传递一个 Uri 对象而不是 String。

public static Bitmap loadBitmap(String filePath, Context c) {
    InputStream inStream;

    try {
        inStream = c.getContentResolver().openInputStream( Uri.parse(filePath) );
    } catch (FileNotFoundException e) {
        // handle file not found
    }

    return BitmapFactory.decodeStream(inStream);
}

这也是 ImageView 小部件在您使用其 setImageURI 方法时尝试加载图像的方式。

【讨论】:

    猜你喜欢
    • 2011-06-16
    • 2017-05-11
    • 2019-11-06
    • 2016-11-13
    • 1970-01-01
    • 2013-04-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多