【发布时间】:2014-07-21 17:00:24
【问题描述】:
在我的应用中,用户可以选择文件并在适当的应用中打开它们(使用 ACTION_VIEW 意图)。
在将数据提供给其他应用程序之前,我需要对数据做一些工作。所以我使用的是流式解决方案:我实现了一个实现openTypedAssetFile 和writeDataToPipe 的ContentProvider(这个方法填充了openPipeHelper 创建的输出ParcelFileDescriptor)。
这很有效:我可以打开 .pdf 文件、.txt 文件等。流式传输似乎是正确的。
我可以使用 3 方应用程序打开图像。
但是,当我使用 Gallery 打开图像时,它不起作用(图库显示黑屏),并且出现以下异常:
fail to open myfile.jpg
UriImage(21890): java.io.FileNotFoundException: Not a whole file
我查看了库源 (here),我可以看到这里抛出了异常:
try {
if (MIME_TYPE_JPEG.equalsIgnoreCase(mContentType)) {
InputStream is = mApplication.getContentResolver()
.openInputStream(mUri);
mRotation = Exif.getOrientation(is);
Utils.closeSilently(is);
}
**mFileDescriptor = mApplication.getContentResolver()
.openFileDescriptor(mUri, "r");**
if (jc.isCancelled()) return STATE_INIT;
return STATE_DOWNLOADED;
} catch (FileNotFoundException e) {
Log.w(TAG, "fail to open: " + mUri, e);
return STATE_ERROR;
}
但是,一旦在图库应用程序中,如果我选择“设置为壁纸”,那么我可以看到我的图像,然后它就会被很好地流式传输。所以问题出现在画廊打开时。
经过深入研究(在 ContentResolver 代码等中),我无法理解它为什么会这样。 Gallery 似乎不支持流文件。那正确吗 ?
你有什么想法吗?
非常感谢。
【问题讨论】:
-
您创建自己的画廊,然后访问选定的图像,github.com/luminousman/MultipleImagePick
标签: android stream android-contentprovider android-contentresolver