SQLiteStatement get = mDb.compileStatement(
"SELECT blobColumn" +
" FROM tableName" +
" WHERE _id = 1" +
" LIMIT 1"
);
ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
FileInputStream fis = new FileInputStream(result.getFileDescriptor()); // read like any other
и я могу убедиться, что это очень быстро для моих случаев использования.
Где я нашел это особенно полезным (если вам нужно было создать свой собственный автономный кэш файлов веб-просмотра, как я сделал), было в переопределении openFile для провайдера контента, где вы можете просто вернуть дескриптор вверх (или в идеале переопределить openTypedAssetFile ))
Примеры:
files является помощником БД, а getFileDescriptor (String) похож на код выше
public AssetFileDescriptor openTypedAssetFile(Uri uri, String mimeTypeFilter, android.os.Bundle opts) throws FileNotFoundException {
ParcelFileDescriptor pfd = files.getFileDescriptor(uri.toString());
if (pfd != null) {
return new AssetFileDescriptor(pfd, 0, AssetFileDescriptor.UNKNOWN_LENGTH);
}
return null;
}
или другое замечательное использование для аналогичной проблемы:
(где вы хотите подать пользовательский файл из БД в веб-просмотр)
private WebViewClient webViewClient = new WebViewClient() {
@Override
public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
ParcelFileDescriptor pfd = files.getFileDescriptor(url);
if (pfd != null) {
return new WebResourceResponse(null, null, new FileInputStream(pfd.getFileDescriptor()));
}
return null;
}