簡體   English   中英

在Nougat上打開PDF

[英]Open PDF on Nougat

我有一個文件路徑:file:///storage/emulated/0/Android/data/rocks.v d 。*****。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.v d 。*****。develop.provider / files / Android / data / rocks.v d 。*****。develop / files / 1Q%202017%20financial%20results .PDF

Pdf閱讀器打開時出現意外錯誤。 怎么了?

intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

在這里,您將清除所有現有標志,特別是您的FLAG_GRANT_READ_URI_PERMISSION 因此,您啟動的活動將無法訪問您的內容。

setFlags()切換到addFlags()

如果你正在使用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上面使用這個代碼。

在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();

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM