【发布时间】:2021-12-27 14:01:39
【问题描述】:
我正在尝试将此文件加载到我的 webview 中,但它没有加载
文件路径类似于
- /data/user/0/com.xyzapp.app/cache/temp_file.docx //这是getpathhh
下面是我的代码
第一个活动:-
val browseintent = Intent(this, WebviewActivityNew::class.java)
browseintent.putExtra("url", "file://"+getpathhh.toString())
startActivity(browseintent)
第二个活动:-
val url = intent.extras!!.getString("url")
webview_comp.getSettings().setLoadsImagesAutomatically(true);
webview_comp.getSettings().setJavaScriptEnabled(true);
webview_comp.getSettings().setAllowContentAccess(true);
webview_comp.getSettings().setAllowFileAccess(true);
webview_comp.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
webview_comp.getSettings().setAppCachePath(url);
webview_comp.getSettings().setAppCacheEnabled(true);
webview_comp.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
url?.let { webview_comp.loadUrl(it) };
如果我使用 Action_view
val pdfOpenintent = Intent(Intent.ACTION_VIEW)
pdfOpenintent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
pdfOpenintent.setDataAndType(Uri.fromFile(getpathhh), "*/*")
try {
startActivity(pdfOpenintent)
} catch (e: ActivityNotFoundException) {
}
我因为“E/UncaughtException: android.os.FileUriExposedException: file:///data/user/0/com.xyzapp.app/cache/temp_file.docx 通过 Intent.getData() 暴露在应用程序之外”而崩溃
按照我的尝试
val pdfOpenintent = Intent(Intent.ACTION_VIEW)
pdfOpenintent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP
pdfOpenintent.setDataAndType(
FileProvider.getUriForFile(
applicationContext,
"com.xyzapp.app.provider",
getpathhh
), "*/*")
try {
startActivity(pdfOpenintent)
} catch (e: ActivityNotFoundException) {
}
【问题讨论】:
-
嗯...出了什么问题?
-
@blackapps 我已经编辑了我的问题
-
你没有用谷歌搜索
FileUriExposedException?Uri.fromFile(getpathhh你不能再使用 Uri.fromFile() 了。你必须使用FileProvider。 -
可以粘贴一些参考资料或为我做一个新的答案吗@blackapps?
-
Google for
FileUriExposedException。
标签: android file kotlin local-storage android-webview