正确使用Android SQLiteStatement的simpleQueryForBlobFileDescriptor()

发布于 2024-12-04 15:13:56 字数 550 浏览 2 评论 0原文

Android 的 SQLiteStatement 中的 simpleQueryForBlobFileDescriptor() 函数对我来说是个谜。而且似乎没有人在任何地方使用它。看起来这对于单个 blob 访问来说是一种快速方法,并且在性能方面(如果文档可信的话),当用于从数据库中流式传输 blob 数据时,SQLiteStatement 和 Parcel 的组合应该会产生非常快的结果。我不知道,因为我无法让这个东西工作,以便我可以测试...对此的任何帮助或见解将不胜感激:

SQLiteStatement get = mDb.compileStatement(
    "SELECT blobColumn" + 
    " FROM tableName" +
    " WHERE _id = 1" +
    " LIMIT 1"
);

ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
// now what?

我可能应该注意到我正在使用并且只真正关心 Honeycomb (3.2)并提前感谢您的任何见解。

The function simpleQueryForBlobFileDescriptor() in Android's SQLiteStatement is a mystery to me. And no one seems to use it anywhere. It seems as though it would be a fast method for individual blob access and in terms of performance (if the documentation is to be believed) the combination of SQLiteStatement and Parcel should yield a very fast result when used to stream blob data out of a db. I don't know because I can't get the thing to work so that I could test... Any help with this or insight would be appreciated:

SQLiteStatement get = mDb.compileStatement(
    "SELECT blobColumn" + 
    " FROM tableName" +
    " WHERE _id = 1" +
    " LIMIT 1"
);

ParcelFileDescriptor result = get.simpleQueryForBlobFileDescriptor();
// now what?

I should probably note that I am using and only really care about Honeycomb (3.2) and thank you in advance for any insight.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

薄荷港 2024-12-11 15:13:56
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

我可以验证这对于我的用例来说非常快。

我发现这特别有用的地方(如果您需要像我一样创建自己的自定义离线 webview 文件缓存)是在内容提供者的 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; 
    }
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

and I can verify that this is very fast for my use cases.

Where I found this particularly useful (if you need to create your own custom offline webview file cache like I did) was in the openFile override for a content provider where you can just return the descriptor straight up (or ideally override openTypedAssetFile())

examples:
files is a DB helper and getFileDescriptor(String) is similar to the code above

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;
}

or another great use for a similar problem:
(where you want to serve custom file from a db to a webview)

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; 
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文