android – 从内容提供商URI查看意图?

我有一些保护一些敏感数据的要求.数据从URL下载为PDF,并使用以下代码保存为应用程序专用文件
public File downloadPDF(final Context fileContext,Uri reportUri,final String fileName)
{
    try
    {
        HttpGet get = new HttpGet(reportUri.toString());

        File file = httpClient.execute(get,new ResponseHandler<File>()
        {
            @Override
            public File handleResponse(HttpResponse response) throws ClientProtocolException,IOException
            {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
                {
                    response.getEntity().writeto(fileContext.openFileOutput(fileName,Context.MODE_WORLD_READABLE));
                    return fileContext.getFileStreamPath(fileName);
                }
                return null;
            }
        });

        return file;
    }
    catch (IOException e)
    {
        Log.e(TAG,"Unable to download report.",e);
    }

    return null;
}

现在,我想做的是将其改为使用Context.MODE_PRIVATE并创建一个ContentProvider,以便我的应用程序完全控制这个文件共享到PDF阅读器,如Adobe Reader.这可能吗?我目前使用如下代码将报告URI传递给当前配置的PDF阅读器.

// Fire up a PDF viewer intent for the URL.
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    intent.setDataAndType(uri,"application/pdf");
    startActivity(intent);

通过ContentProvider类型URI可以同样的工作吗?一个内容:// package / fileid type URI?明天我会尝试一点点,看看是否可以,但是如果有人知道只有文件:// URI被允许,这将是非常有帮助的.

UPDATE

通过实现一个ContentProvider子类,我能够令人满意地解决我的问题,覆盖了以下方法

@Override
public ParcelFileDescriptor openFile(Uri uri,String mode) throws FileNotFoundException 
{
    // The filename is the path in the URI without the initial slash.
    String fileName = uri.getPath().substring(1);
    File file = getContext().getFileStreamPath(fileName);
    return ParcelFileDescriptor.open(file,ParcelFileDescriptor.MODE_READ_ONLY);
}

然后,当我消除观看意图时,它被重写成如下:

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
Uri uri = Uri.withAppendedpath(Uri.parse("content://providername/"),filePath);
intent.setData(uri);
startActivity(intent);

在我的情况下,我使用Adobe Reader,它正确地从content:// URIs实现加载.

解决方法

Would the same work through a ContentProvider type URI? A content://package/fileid type URI?

这应该.您将需要让ContentProvider返回应用程序/ pdf for getType().而且,有些PDF阅读器可能无法处理内容:// Uri值.

您可以做的是使ContentProvider,然后使用PackageManager来查看是否有任何内容将了解您的ACTION_VIEW Intent内容:// Uri.如果有什么回应,你就被设置了.如果没有,您可以回到使文件变得世界可读,并使用您当前的实现.或者,由于将文件更改为世界可读可能会很痛苦,您可以尽早运行测试(例如,当您的应用程序启动时)与您的ContentProvider支持的一些废品Uri一起使用,因此您将了解当您执行哪个路由时你的下载

相关文章

这篇“android轻量级无侵入式管理数据库自动升级组件怎么实现...
今天小编给大家分享一下Android实现自定义圆形进度条的常用方...
这篇文章主要讲解了“Android如何解决字符对齐问题”,文中的...
这篇文章主要介绍“Android岛屿数量算法怎么使用”的相关知识...
本篇内容主要讲解“Android如何开发MQTT协议的模型及通信”,...
本文小编为大家详细介绍“Android数据压缩的方法是什么”,内...