【问题标题】:Raw File path access of file at JavaScript in Android 11.(scoped-storage)Android 11 中 JavaScript 文件的原始文件路径访问。(范围存储)
【发布时间】:2020-10-21 16:06:11
【问题描述】:

https://developer.android.com/training/data-storage#scoped-storage

设备名称: 用户代理:Mozilla/5.0 (Linux; Android 11; Pixel 3a Build/RPB1.200504.020; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/83.0.4103.106 Mobile Safari/537.36

1)android targetSdkVersion 30 requestLegacyExternalStorage = false。

android {
    compileSdkVersion 30
    defaultConfig {
        applicationId "com.example.ui"
        minSdkVersion 21
        targetSdkVersion 30
        }
     }
    }
  1. 文件路径或 URL lFileUrl = file:///data/user/0/com.example.ui/files/KK&T SEW_temp/IMG-20200630-223714.jpg

在基于cordova的android应用程序中从javascript级别完成的XMLHttpRequest请求。

var xhr = new XMLHttpRequest();
xhr.open("GET", lFileUrl, true);
xhr.responseType = "blob";
UI.Util.info("PtxUtils.getInlineAttachmnetJson() >> inside xhr.open");
xhr.onload = function(e) {
try {
UI.Util.info("PtxUtils.getInlineAttachmnetJson() >> inside xhr.onload");
}
                

3)XMLHttpRequest 抛出错误

ERR_ACCESS_DENIED, when getFilesDir() API used.
PtxUtils.js:2950 GET 

file:///data/user/0/com.example.ui/files/KK&T%20SEW_temp/IMG-20200630-223714.jpg net::ERR_ACCESS_DENIED

23:09:57.058 PtxUtils.js:2950 XHR failed loading: GET "file:///data/user/0/com.example.ui/files/KK&T%20SEW_temp/IMG-20200630-223714.jpg".

4)XMLHttpRequest 抛出错误

ERR_ACCESS_DENIED, when getExternalFilesDir()() API used.

PtxUtils.js:2950 GET file:///storage/emulated/0/Android/data/com.example.ui/files/KK&T%20SEW_temp/IMG-20200701-135735.jpg net::ERR_ACCESS_DENIED

PtxUtils.js:2950 XHR failed loading: GET "file:///storage/emulated/0/Android/data/com.example.ui/files/KK&T%20SEW_temp/IMG-20200701-135735.jpg".    

5)android targetSdkVersion 29 requestLegacyExternalStorage = false

with getFilesDir() path getExternalFilesDir() XMLHttpRequest is pass with no error, file data is retrieved.




                        

【问题讨论】:

标签: javascript android file cordova xmlhttprequest


【解决方案1】:

在我看来,您可能需要以下权限:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

为了更好的衡量,我也会尝试:

<uses-permission android:name="android.permission.INTERNET"/>

并将其放入清单中:

<application
    android:usesCleartextTraffic="true">
</application>

此外,在您进行更改清除数据后,卸载应用程序,重新安装,然后重试。

最后,如果你在任何地方使用 webview,也可以这样做:

webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setAllowFileAccess(true);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-06-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-25
    • 2016-08-17
    相关资源
    最近更新 更多