【问题标题】:BitmapFactory.decodeStream is returning null: AndroidBitmapFactory.decodeStream 返回 null:Android
【发布时间】:2014-04-08 10:34:23
【问题描述】:

我在第二个类 (Class2) 中有一个方法,我从 Class1 中传入上下文和 uri 的值。第二类 (Class2) 是一个类,其中过滤器将应用于使用 Class1 中的相机拍摄的图像。 Class2 中的方法看起来像这样

public void prepareImage(Context context, Uri uri) {

    // BitmapFactory options
    Options options = new Options();
    options.inSampleSize = 2;

    String path = uri.toString();
    File file = new File(path);

        FileInputStream fis = null; //initialize
        try {
            fis = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        Bitmap img = BitmapFactory.decodeStream(fis);
        Log.i(TAG, "Bitmap img: " + img);
                    //more code below, but above this comment is where the issue is

}

我注意到 FileInputStream 返回 null,因此 BitmapFactory.decodeStream 没有解码。我认为有两件事是问题,但不知道如何解决。首先,我想知道我是否收到 FileInputStream 的空值,因为我使用的是由 uri.toString() 形成的路径。或者那有关系吗?字符串路径值正确。

第二个问题是,如果我的手机连接到计算机,在使用我的应用程序拍摄图像后,图像文件似乎直到我从计算机上拔下设备后才注册。只有在我从计算机上拔下我的设备后,我的所有图像才会出现在画廊文件夹中。所以我怀疑是找不到文件! logcat 有证据证明这一点。

无论如何,我不知道如何解决这个冲突。也许我需要以不同的方式保存图像。这是我在 Class1 中保存图像的方法。

File imgFileDir = getDir();
            if (!imgFileDir.exists() && !imgFileDir.mkdirs()) {
                Log.e(TAG, "Directory does not exist");
            }

        //Locale.US to get local formatting
        SimpleDateFormat timeFormat = new SimpleDateFormat("hhmmss", Locale.US);
        SimpleDateFormat dateFormat = new SimpleDateFormat("ddMMyyyy", Locale.US);
        String time = timeFormat.format(new Date());
        String date = dateFormat.format(new Date());
        String photoFile = date + "_nameofapp_" + time + ".jpg";       
        String filename = imgFileDir.getPath() + File.separator + photoFile;

        File pictureFile = new File(filename);

        try {
            FileOutputStream fos = new FileOutputStream(pictureFile);
            fos.write(arg0);
            fos.flush();
            fos.close();
        } catch (Exception e) {
            Log.e(TAG, "Image could not be saved");
            e.printStackTrace();
        }

我也使用辅助方法

private File getDir() {
        File sdDir = Environment.
                getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
        return new File(sdDir, "NameOfApp");
    }

最后,我尝试在不受限制的情况下运行该应用程序以查看它是否可以工作,但它不能。行为是一样的,我拍摄的图像不会显示在画廊中,直到我将设备重新插入我的计算机,然后我拍摄的所有图像都会显示在画廊中。为什么?

根据请求,我从 Class1 传递 URI,执行以下操作。

Class2 classTwo = new Class2();
classTwo.prepareImage(this, uri);

【问题讨论】:

  • 你能告诉你你在哪里打电话给prepareImage(),你用什么Uri传递给它?
  • 嗨贾斯汀,我更新了我的帖子。你的问题的答案在最后。 Uri 是公共的和静态的,我实际上尝试使用 setter/getter 执行此代码,以双重确认 Uri 正在达到 Class2。它就在那里。
  • 什么是 Uri?在您的 logcat 中,它看起来像是一个额外的 / 进入了那里。 file:/storage/emulated/0 .../file:/storage/emulated/0 ...(来自异常消息)。
  • 贾斯汀,你是天才。这就是问题所在。修复方法不是使用 Uri.fromFile() 获取 URI,而是使用 Uri.parse()。两者的区别在于fromFile()多了一个/。谢谢人,这现在很完美!如果您创建答案,我也会接受。
  • 完成了 :) 很高兴我能帮上忙!有时,当您整天查看代码时(我自己经历过太多次),这些细节会变得模糊。

标签: android bitmap camera


【解决方案1】:

只是将其移至答案,以便每个人都可以看到:

您的文件 Uri 中似乎有一个额外的 /

file:/storage/emulated/0 ... 

对比

/file:/storage/emulated/0 ... (from the exception message)

:)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-25
    • 2011-05-23
    • 1970-01-01
    • 1970-01-01
    • 2011-01-31
    • 1970-01-01
    相关资源
    最近更新 更多