Android:在奥利奥开放PDF

时间:2018-04-24 01:05:17

标签: android androidpdfviewer

伙计我在使用Version Oreo的项目中打开PDF时遇到问题,但我的代码在其他操作系统版本上运行良好。我在ListView中有一个PDF列表,onClick它应该在外部打开PDF,我试图直接打开它并通过createChooser但在Oreo都有问题。

注意:许可(已检查),PDFViewer应用程序(已安装)

以下是我的代码:

doc_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Uri path = Uri.fromFile(files.get(position));
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(path, "application/pdf");
                intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                try{
                    Intent intent1 = Intent.createChooser(intent, "Open With");
                    startActivity(intent1);
                } catch (ActivityNotFoundException e) { }

            }
        });




04-24 02:33:57.301 2913-2913/com.tabian.tabfragments E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.tabian.tabfragments, PID: 2913
    android.os.FileUriExposedException: file:///storage/emulated/0/eFSIS/InspectionOrder/TestNew/Kyoto/InspectionOrder.pdf exposed beyond app through Intent.getData()
        at android.os.StrictMode.onFileUriExposed(StrictMode.java:1958)
        at android.net.Uri.checkFileUriExposed(Uri.java:2348)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:9766)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:9741)
        at android.content.Intent.prepareToLeaveProcess(Intent.java:9720)
        at android.app.Instrumentation.execStartActivity(Instrumentation.java:1609)
        at android.app.Activity.startActivityForResult(Activity.java:4472)
        at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult(BaseFragmentActivityApi16.java:54)
        at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:67)
        at android.support.v4.app.ActivityCompat.startActivityForResult(ActivityCompat.java:152)
        at android.support.v4.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:793)
        at android.support.v4.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:907)
        at android.support.v4.app.Fragment.startActivity(Fragment.java:1009)
        at android.support.v4.app.Fragment.startActivity(Fragment.java:998)
        at com.tabian.tabfragments.Tabs.DocumentFragment$1.onItemClick(DocumentFragment.java:65)
        at android.widget.AdapterView.performItemClick(AdapterView.java:318)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1165)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3134)
        at android.widget.AbsListView$3.run(AbsListView.java:4049)
        at android.os.Handler.handleCallback(Handler.java:789)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6541)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
04-24 02:33:57.313 471-487/system_process E/memtrack: Couldn't load memtrack module
04-24 02:33:57.324 331-356/? E/AudioStreamOutSink: Error while writing data to HAL: -32
04-24 02:33:57.346 331-356/? E/AudioStreamOutSink: Error while writing data to HAL: -32
04-24 02:33:57.362 471-1492/system_process E/EGL_emulation: tid 1492: eglSurfaceAttrib(1210): error 0x3009 (EGL_BAD_MATCH)
04-24 02:33:57.386 331-356/? E/AudioStreamOutSink: Error while writing data to HAL: -32
04-24 02:33:57.587 331-356/? E/AudioStreamOutSink: Error while writing data to HAL: -32
04-24 02:33:57.627 331-356/? E/AudioStreamOutSink: Error while writing data to HAL: -32
04-24 02:34:07.813 471-487/system_process E/memtrack: Couldn't load memtrack module

at com.tabian.tabfragments.Tabs.DocumentFragment$1.onItemClick(DocumentFragment.java:65) //startActivity(intent1);

2 个答案:

答案 0 :(得分:1)

SDK版本24发生了变化。当您的targetSdkVersion >= 24必须使用FileProvider类来授予对特定文件的访问权限,以便可以从其他应用访问它。

有关详细信息,请参阅此SO

答案 1 :(得分:0)

 use this lib:
 "https://github.com/barteksc/AndroidPdfViewer"


Glide:
compile 'com.github.barteksc:android-pdf-viewer:3.0.0-beta.5'

<com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>