我尝试在奥利奥打开pdf文件,但它没有打开.我没有得到任何错误.有什么问题? pdf文件无法打开.仅显示黑屏.在logcat中没有错误显示.怎么了?
我该如何解决这个问题?我提到了许多链接,但没有得到解决方案.我也试过很多代码,但没有帮助.
我的代码是:
file file11 = new file(Environment.getExternalStorageDirectory(). getabsolutePath(), "Atmiyaimages/" + nameoffile1); Intent target = new Intent(Intent.ACTION_VIEW); target.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); if(Build.VERSION.SDK_INT>24){ Uri uri=fileProvIDer.getUriForfile(AssignmentActivity.this, getPackagename()+".provIDer",file11); target.addFlags(Intent.FLAG_GRANT_PERSIStable_URI_PERMISSION); target.putExtra(Intent.EXTRA_STREAM,uri); target.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); target.setType("application/pdf"); Intent intent=Intent.createChooser(target,"Open file"); try { startActivity(intent); } catch(ActivityNotFoundException e) { Toast.makeText(AssignmentActivity.this,"No Apps can performs This acttion",Toast.LENGTH_LONG).show(); } } else { target.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); target.setDataAndType(Uri.fromfile(file11),"application/pdf"); target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); Intent intent=Intent.createChooser(target,"Open file"); try { startActivity(intent); } catch(ActivityNotFoundException e) { Toast.makeText(AssignmentActivity.this,"No Apps can performs This acttion",Toast.LENGTH_LONG).show(); } }
在Manifest中我也补充说
<provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="com.infinity.infoway.atmiya.fileprovIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/provIDer_paths"/> </provIDer>
我的Xml代码是:
<?xml version="1.0" enCoding="utf-8"?><paths><external-path name="Phonestorage"path="/storage/emulated/0/Atmiyaimages"/></paths>
解决方法:
用“.”替换路径值. – 看下面:
<?xml version="1.0" enCoding="utf-8"?><paths xmlns:androID="http://schemas.androID.com/apk/res/androID"> <external-path name="external_files" path="."/></paths>
在Manifest中,“提供者”代码就是这样,你的代码也是如此.
<provIDer androID:name="androID.support.v4.content.fileProvIDer" androID:authoritIEs="${applicationID}.provIDer" androID:exported="false" androID:grantUriPermissions="true"> <Meta-data androID:name="androID.support.file_PROVIDER_PATHS" androID:resource="@xml/provIDer_paths" /> </provIDer>
要访问正确的文件路径,请遵循以下代码
file pdffile = new file(getExternalfilesDir(GlobalUtils.AppFolder), filename); Uri path = fileProvIDer.getUriForfile(this, BuildConfig.APPliCATION_ID + ".provIDer", pdffile); Log.e("create pdf uri path==>", "" + path); try { Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(path, "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_top); intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); startActivity(intent); finish(); } catch (ActivityNotFoundException e) { Toast.makeText(getApplicationContext(), "There is no any pdf VIEwer", Toast.LENGTH_SHORT).show(); finish(); }
希望这会帮助你.
总结以上是内存溢出为你收集整理的android – 如何从奥利奥的内部存储中打开PDF文件?全部内容,希望文章能够帮你解决android – 如何从奥利奥的内部存储中打开PDF文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)