我的PDF文件在新的Android版本上无法打开

时间:2019-08-07 19:16:51

标签: java android pdf

我有一个本地托管了多个PDF文件的应用程序。几年前它们都打开得很好,但是现在我想对其进行更新,但是它不再在新的Android版本上打开。

我尝试了一些在这里找到的答案,但它不起作用。

打开文件的步骤如下:

public void abrirLey(String nombreLey) {
        File fileBrochure = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + nombreLey);
        if (!fileBrochure.exists()) {
            CopyAssetsbrochure(nombreLey);
        }
        /** PDF reader code */
        File file = new File(Environment.getExternalStorageDirectory()
                .getAbsolutePath() + "/" + nombreLey);
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(Uri.fromFile(file), "application/pdf");
        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        try {
            getApplicationContext().startActivity(intent);
        } catch (ActivityNotFoundException e) {
            Toast.makeText(Leyes.this,
                    "Debe tener instalado un lector de archivos PDF.",
                    Toast.LENGTH_LONG).show();
        }
    }

这也是将文件从Assets文件夹复制到手机存储的方法:

 public void CopyAssetsbrochure(String nombreLey) {
        AssetManager assetManager = getResources().getAssets();
        String[] files = null;
        try {
            files = assetManager.list("leyes/leyesPDF");
        } catch (IOException e) {
            Log.e("tag", e.getMessage());
        }
        for (int i = 0; i < files.length; i++) {
            String fStr = files[i];
            if (fStr.equalsIgnoreCase(nombreLey)) {
                InputStream in = null;
                OutputStream out = null;
                try {
                    in = assetManager.open("leyes/leyesPDF/" + nombreLey);
                    out = new FileOutputStream(Environment
                            .getExternalStorageDirectory().getAbsolutePath()
                            + "/" + nombreLey);
                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                    break;
                } catch (Exception e) {
                    Log.e("tag", e.getMessage());
                }
            }
        }
    }
public void copyFile(InputStream in, OutputStream out) throws IOException {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1) {
            out.write(buffer, 0, read);
        }
    }

每当我单击按钮打开PDF文件时,应用程序就会停止工作并保持关闭状态。请记住,几年前,同样的代码在我的应用程序中也可以使用。

任何帮助将不胜感激。 谢谢。

--- 编辑:这是我的日志。

2019-08-07 15:24:27.153 30994-30994/com.otbal.abogadom E/tag: /storage/emulated/0/LeyNo17_19.pdf (Permission denied)
2019-08-07 15:24:27.154 30994-30994/com.otbal.abogadom D/AndroidRuntime: Shutting down VM
2019-08-07 15:24:27.155 30994-30994/com.otbal.abogadom E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.otbal.abogadom, PID: 30994
    android.os.FileUriExposedException: file:///storage/emulated/0/LeyNo17_19.pdf exposed beyond app through Intent.getData()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
        at android.net.Uri.checkFileUriExposed(Uri.java:2371)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10936)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10887)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
        at android.app.ContextImpl.startActivity(ContextImpl.java:979)
        at android.app.ContextImpl.startActivity(ContextImpl.java:950)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:384)
        at com.otbal.abogadom.Leyes.abrirLey(Leyes.java:632)
        at com.otbal.abogadom.Leyes$54.onClick(Leyes.java:515)
        at android.view.View.performClick(View.java:7352)
        at android.widget.TextView.performClick(TextView.java:14177)
        at android.view.View.performClickInternal(View.java:7318)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27800)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)
2019-08-07 15:24:31.160 30994-30994/com.otbal.abogadom W/GAv4: syncDispatchLocalHits timed out: java.util.concurrent.TimeoutException
2019-08-07 15:24:31.164 30994-30994/com.otbal.abogadom E/UncaughtException: android.os.FileUriExposedException: file:///storage/emulated/0/LeyNo17_19.pdf exposed beyond app through Intent.getData()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1978)
        at android.net.Uri.checkFileUriExposed(Uri.java:2371)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10936)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:10887)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1667)
        at android.app.ContextImpl.startActivity(ContextImpl.java:979)
        at android.app.ContextImpl.startActivity(ContextImpl.java:950)
        at android.content.ContextWrapper.startActivity(ContextWrapper.java:384)
        at com.otbal.abogadom.Leyes.abrirLey(Leyes.java:632)
        at com.otbal.abogadom.Leyes$54.onClick(Leyes.java:515)
        at android.view.View.performClick(View.java:7352)
        at android.widget.TextView.performClick(TextView.java:14177)
        at android.view.View.performClickInternal(View.java:7318)
        at android.view.View.access$3200(View.java:846)
        at android.view.View$PerformClick.run(View.java:27800)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7037)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:494)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:965)

0 个答案:

没有答案