Android:在 Oreo 中打开 PDF
Android: Opening PDF in Oreo
伙计们,我在使用 Version Oreo 的项目中打开 PDF 时遇到问题,但我的代码在其他 OS 版本上运行良好。我的 ListView 中有一个 PDF 列表,单击它应该在外部打开 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.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.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.onItemClick(DocumentFragment.java:65) //startActivity(intent1);
SDK 版本 24 发生了变化。当您 targetSdkVersion >= 24
时,您必须使用 FileProvider
class 来授予对特定文件的访问权限,以便其他人可以访问该文件应用程序。
参考这篇了解详情
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"/>
伙计们,我在使用 Version Oreo 的项目中打开 PDF 时遇到问题,但我的代码在其他 OS 版本上运行良好。我的 ListView 中有一个 PDF 列表,单击它应该在外部打开 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.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.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.onItemClick(DocumentFragment.java:65) //startActivity(intent1);
SDK 版本 24 发生了变化。当您 targetSdkVersion >= 24
时,您必须使用 FileProvider
class 来授予对特定文件的访问权限,以便其他人可以访问该文件应用程序。
参考这篇
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"/>