在Nougat上打开PDF

5

我有一个文件路径:file:///storage/emulated/0/Android/data/rocks.vd.*****.develop/files/1Q 2017财务报告.pdf

这是代码:

Intent intent = new Intent(Intent.ACTION_VIEW);
    try {
        String newFilePath = filePath.replaceAll("%20", " ");
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
            intent.setDataAndType(Uri.parse(newFilePath), "application/pdf");
        } else {
            Uri uri = Uri.parse(newFilePath);
            File file = new File(uri.getPath());
            if (file.exists()){
                uri = FileProvider.getUriForFile(context, context.getPackageName() + ".provider", file);
                intent.setDataAndType(uri, "application/pdf");
                intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            }
        }

        intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        context.startActivity(intent);

我的uri = content://rocks.vd.*****.develop.provider/files/Android/data/rocks.vd.*****.develop/files/1Q%202017%20financial%20results.pdf

PDF阅读器打开时出现意外错误。 发生了什么问题?


问题只出现在Nougat设备上吗? - Anmol317
@Anmol 是的,只有nougat。 - Alex
@Anmol,谢谢你,我发现了一个 bug: intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);READ_URI_PERM 标志被 NO_HISTORY 取代)) - Alex
3个回答

6
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

在这里,您正在清除所有现有标志,特别是您的FLAG_GRANT_READ_URI_PERMISSION。结果,您启动的活动将无法访问您的内容。
请从setFlags()切换到addFlags()

@CommonsWare 我尝试了这个,但还是不起作用。你能给我一些建议或完整的代码吗? - Passionate.C
@Passionate.C:我不知道“仍然无法工作”是什么意思。我建议您在单独的Stack Overflow问题中提问,其中提供一个[mcve]并详细解释您的问题是什么。 - CommonsWare
@CommonsWare 我发布了一个新问题。https://stackoverflow.com/questions/48103496/xamarin-android-open-file-using-default-app-not-work-only-android-7-or-above 你能否帮我检查并帮助我? - Passionate.C

3
如果您正在使用 Android-N(即API 24及以上版本),请按照以下步骤操作:
try {
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    File f = new File(Your File Path);

    Uri uri = null;


    // So you have to use Provider
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
        uri = FileProvider.getUriForFile(this,getApplicationContext().getPackageName() + ".provider", file);

        // Add in case of if We get Uri from fileProvider.
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    }else{
        uri = Uri.fromFile(f);
    }

    intent.setDataAndType(uri, "application/pdf");
    startActivity(intent);
} catch(Exeption e){
    e.printstacktrace();
}

您也可以在Lollipop及以上版本中使用此代码。


-1

在Adobe pdf阅读器中打开它。如果未安装Adobe Reader,则将其重定向到Google Play。

 private void viewPdf(Uri file){
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/pdf");
try{
startActivity(intent);
}catch(ActivityNotFoundException e){
AlertDialog.Builder builder = new AlertDialog.Builder(getApplicationContext());
builder.setTitle("No Application Found");
builder.setMessage("Download from Android Market?");
builder.setPositiveButton("Yes, Please", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub

Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);

}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();

}

我有这些代码块() - Alex
你的日志里有任何错误吗?或者你正在尝试在哪个应用程序中打开你的PDF文件? - Anmol317
没有,我没有任何错误。有两个免费的PDF阅读器。我在模拟器上尝试了一下。 - Alex
我们需要什么权限吗? - Alex
1
@Anmol:这段代码在Android 7.0+设备上无法正常工作。如果Uri有一个file方案,那么你的代码是正确的,但是你不能在Android 7.0+上使用它。如果Uri有一个content方案,如来自FileProvider,则未将FLAG_GRANT_READ_URI_PERMISSION添加到Intent中。 - CommonsWare
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接