Tho*_*ide 11 android android-contentprovider
我有一些要求来保护一些敏感数据.数据从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;
}
Run Code Online (Sandbox Code Playgroud)
现在,我想要做的是将其更改为使用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);
Run Code Online (Sandbox Code Playgroud)
同样的工作是通过ContentProvider类型的URI吗?content:// package/fileid类型的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);
}
Run Code Online (Sandbox Code Playgroud)
然后,当我关闭查看意图时,它会被重写为如下所示:
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);
Run Code Online (Sandbox Code Playgroud)
在我的例子中,我使用Adobe Reader,它正确地实现了从content://URI 加载.
同样的工作是通过ContentProvider类型的URI吗?content:// package/fileid类型的URI?
这应该.你需要让你的ContentProvider回报application/pdf了getType().并且,某些PDF阅读器可能无法处理content:// Uri值.
你可以做的是制作ContentProvider,然后PackageManager用来看看是否有什么能理解你ACTION_VIEW Intent的content:// Uri.如果有什么东西响应,你就会被设定.如果没有,您可以回过头来使文件具有世界可读性并使用您当前的实现.或者,由于将文件更改为世界可读可能会很麻烦,您可以尽早运行测试(例如,当您的应用程序启动时)使用Uri您ContentProvider支持的一些剪贴簿,这样您就可以知道下载时要采取的路线.
| 归档时间: |
|
| 查看次数: |
5010 次 |
| 最近记录: |