【问题标题】:Java android bitmap = null for certain imagesJava android bitmap = null 对于某些图像
【发布时间】:2017-09-08 06:42:18
【问题描述】:

我正在制作一个允许用户拍照的 android 应用程序,然后该应用程序将打印一些 RGB 值等。我正在保存手机上拍摄的照片,然后从这些 png 文件中制作位图。我刚刚发现我应该让应用程序休眠一会儿才能保存图像。但是对于我拍摄的某些图像,我仍然认为位图为空。如果我用 6 种不同颜色拍摄魔方的图像,我几乎永远不会得到空指针异常。但是,如果我拍摄墙壁或其他东西的照片,则位图 = null。

有谁知道我应该怎么做才能解决这个问题?

Bitmap myBitmap;
final String dir =  
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + 
"/picFolder/";
try{
    file = dir+Integer.toString(side)+".jpg";
    File f = new File(file);
    options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    myBitmap = BitmapFactory.decodeFile(file,options);

    for(int i = 0; i<3; i++){
        for(int j = 0; j<3; j++){
            cube[side-1][i][j] = getColor(myBitmap, i, j);
        }
    }
}catch (Exception e){
    Log.e("er0r", "HERE:::: " + e.toString());

}

【问题讨论】:

  • 请贴一些代码。 “我正在保存手机拍摄的照片”是什么意思?你在控制相机吗?有一个意图返回图像。
  • 我现在添加了代码。该应用程序打开相机并允许用户拍照并将其保存在手机上。然后应用程序尝试从照片中制作位图,但有时它为空。
  • 您遇到错误了吗?你怎么知道 myBitmap 为空?
  • 更改,Log.e("er0r", "HERE:::: " + e.toString());Log.e("er0r", "HERE:::: " + e.toString(), e); 然后重新创建问题并将 logcat 中的堆栈跟踪添加到您的问题中
  • 好吧,在文档中 inJustDecodeBounds - If set to true, the decoder will return null (no bitmap), but the out...

标签: java android image bitmap null


【解决方案1】:

在我的应用中开发相机时,我也遇到了同样的问题。 对于某些图像,它工作正常,而对于某些图像,它显示为空。 后来我发现这是一个尺寸问题。 我这样解决了这个问题,

private static Bitmap compressBitmap(Bitmap original) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        original.compress(Bitmap.CompressFormat.JPEG, 100, out);
        Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));
        return decoded;
    }

如果您需要任何其他帮助,请告诉我。

【讨论】:

  • 谢谢,它几乎起作用了:P 我相信它现在更频繁地起作用了,但是,当我为墙壁或某物拍照时,我得到了这个异常:HERE:::: java.lang。 NullPointerException:尝试在空对象引用上调用虚拟方法 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)'。
  • 顺便说一下,这是我应用您的代码的方式:“ ByteArrayOutputStream out = new ByteArrayOutputStream(); myBitmap = BitmapFactory.decodeFile(file,options); myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); myBitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray())); "
  • please accept my answer and upvote it. ??你的答案是完全错误的。它不处理来自 decodeFile() 的空位图。我想知道为什么 OP 将这种废话标记为正确。我希望他能撤销它。并投票否决。
  • 没错,他的回答没有帮助。我自己修好了。问题是我试图在图像完成保存之前从图像创建位图。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-08-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多