打开外部存储中的 PDF 文件并在按钮点击时使用默认的 PDF 查看器应用程序。

huangapple go评论68阅读模式
英文:

opening pdf file in external storage in default pdf viewer app on a button click

问题

以下是您提供的代码的翻译部分:

在按钮点击下面以下函数被执行当提示时我授予了存储权限如果没有StartActivity行它就不会崩溃有人可以帮我吗我想要用设备默认的PDF查看器打开一个特定的现有文件权限文件存在或PDF查看器应用程序的存在都没有错误

public void openfile(View v) {

    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
    } else {

        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "notes.pdf");  // -> 文件名 = notes.pdf
        if (pdfFile.exists()) {

            Uri path = Uri.fromFile(pdfFile);
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(pdfIntent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "没有可用于查看PDF的应用程序", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(NewActivity.this, "不存在此文件", Toast.LENGTH_SHORT).show();
        }
    }
}

请注意,上述翻译仅包括代码部分,不包括任何其他内容。

英文:

on a button click below function is executed , I gave it storage permission when prompted to. without the StartActivity line it's not crashing , can anyone help me with this , i want to open a specific existing file with device default pdf viewer. ther is no error with permissions or file existance or pdf viwer application presence.

public void openfile(View v) {

    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, MY_PERMISSIONS_REQUEST_READ_MEDIA);
    } else {

        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "notes.pdf");  // -> filename = maven.pdf
        if (pdfFile.exists()) {

            Uri path = Uri.fromFile(pdfFile);
            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(pdfIntent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(NewActivity.this, "No such file exists", Toast.LENGTH_SHORT).show();
        }
    }
}

I tried a lot of times , it didn't even shown a "no application found to view pdf " error
straightaway its crashing

答案1

得分: 1

I got the answer , now its working , Had to use FileProvider , now it's more like

if anyone facing same problem let me know I will post step by step solution

public void openfile(View v) {

    if (!checkstoragepermission()) {
        requestReadpermission();
    }else {
        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio app/" + "/materials/"+"notes.pdf");  // -> filename = maven.pdf
        if (pdfFile.exists()) {

            File imagePath = new File(Environment.getExternalStorageDirectory() + "/" + "Zio app" ,"materials");
            File newFile = new File(imagePath, "notes.pdf");
            Uri path = FileProvider.getUriForFile(this, "com.example.zio.fileprovider", newFile);

            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Intent intent = Intent.createChooser(pdfIntent, "Open File");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);

            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "No Application available to view PDF", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(NewActivity.this, "No such file exists", Toast.LENGTH_SHORT).show();
        }

    }

}

英文:

finally I got the answer , now its working , Had to use FileProvider , now it's more like

if anyone facing same problem let me know I will post step by step solution

public void openfile(View v) {

    if (!checkstoragepermission()) {
        requestReadpermission();`
    }else {
        File pdfFile = new File(Environment.getExternalStorageDirectory() + "/Zio 
       app/" + "/materials/"+"notes.pdf");  // -> filename = maven.pdf
        if (pdfFile.exists()) {

            File imagePath = new File(Environment.getExternalStorageDirectory() + "/" 
            + "Zio app" ,"materials");
            File newFile = new File(imagePath, "notes.pdf");
            Uri path = FileProvider.getUriForFile(this, 
             "com.example.zio.fileprovider", newFile);

            Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
            pdfIntent.setDataAndType(path, "application/pdf");
            pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 
            Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);
            Intent intent = Intent.createChooser(pdfIntent, "Open File");
            intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | 
            Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_ACTIVITY_CLEAR_TOP);


            try {
                startActivity(intent);
            } catch (ActivityNotFoundException e) {
                Toast.makeText(getApplicationContext(), "No Application available to 
                 view PDF", Toast.LENGTH_SHORT).show();
            }
        } else {
            Toast.makeText(NewActivity.this, "No such file exists", 
              Toast.LENGTH_SHORT).show();
        }

    }

}

huangapple
  • 本文由 发表于 2020年8月2日 19:21:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/63215396.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定