【问题标题】:query file uri with content resolver?使用内容解析器查询文件 uri?
【发布时间】:2015-03-09 09:41:40
【问题描述】:

我有以下简单的代码用于调查图像的方向。

Cursor cursor = getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

当 photoUri 是内容 uri 时,即从 content:// 开始,光标可以正常检索。当我从图库中选择图像时,我得到一个 contentUri。

但是,当 photoUri 是 fileUri 时,即从 file://a/b/c.jpg 开始(当我从相机捕获图像时会发生这种情况),光标为 null 。

什么给了?我真的对这种情况感到困惑。

【问题讨论】:

  • 这是正常的预期行为。你有什么问题?
  • 好吧,我也希望能够查询 fileuri。我也需要从 fileuri 定义的图片中获取光标。有什么指点吗?

标签: android


【解决方案1】:

回复评论:

你不能。 MediaStore.Images.ImageColumns.ORIENTATION 是存储在content:// 数据库中的数据库列。 file://数据库不包含这样的列,所以无法查询。

回答问题:

为了提取方向,android有原生的ExifInterface类:https://developer.android.com/reference/android/media/ExifInterface.html

ExifInterface exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

【讨论】:

  • 这有点清楚了。最后一点。我之前实际上是在使用 ExifInterface 。但是我听说它在大多数设备上都不能正常工作,只能在一些设备上工作。因此我恢复了游标技术。您认为使用 Exif 安全吗?
  • 并非如此。上面有不少bug。但我相信content:// DB 无论如何都会从ExifInterface 生成其数据。我从经验中知道 Picasso 库 (square.github.io/picasso) 可以完美地处理方向,也许你应该检查他们的源代码,看看他们是怎么做的。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-23
  • 2014-06-06
相关资源
最近更新 更多