文件输入流在Api级别29上不起作用

时间:2019-10-04 16:42:56

标签: java android android-studio m3u

我有一个尝试提取m3u文件的方法,但是在api级别29下不起作用。在Logcat屏幕上出现以下错误:

2019-10-03 20:46:53.165 32591-417/? E/Google: java.io.IOException: Cleartext HTTP traffic to mysite.tk not permitted

我的方法:

@SuppressLint("StaticFieldLeak")
class _loadFile extends AsyncTask<String, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        spinner.setVisibility(View.VISIBLE);
    }

    @Override
    protected Boolean doInBackground(String... strings) {
        try { //new FileInputStream (new File(name)
            is = new FileInputStream(new File(strings[0])); // if u r trying to open file from asstes InputStream is = getassets.open(); InputStream
            M3UPlaylist playlist = parser.parseFile(is);
            mAdapter.update(playlist.getPlaylistItems());

            return true;
        } catch (Exception e) {
            Log.d("Google", "_loadFile: " + e.toString());
            return false;
        }
    }

    @Override
    protected void onPostExecute(Boolean aBoolean) {
        super.onPostExecute(aBoolean);
        spinner.setVisibility(View.GONE);
    }
}

EDİT:

我授予ClearText权限,但出现了一个新错误。

Google: _loadFile: java.io.FileNotFoundException: /storage/emulated/0/Netuptv/data.m3u: open failed: ENOENT (No such file or directory)

EDİT:

我的文件路径。我认为问题就在这里。但是我没有解决问题的信息。

   static final File DEFA = Environment.getExternalStorageDirectory();
    public static final File dir = new File(DEFA.getPath() + "/Netuptv");
    static final File filepath = new File(dir.getPath() + "/data.m3u");

3 个答案:

答案 0 :(得分:0)

不建议使用Android 10(API级别29)文件访问,它称为scoped storage

答案 1 :(得分:0)

请检查是否已将此行android:usesCleartextTraffic="true"添加到清单中。

由于Android Q上已弃用文件访问API,因此我强烈建议您阅读 CommaneWare article,如本文所述,请尝试使用{{1} },以避免文件访问问题

尝试以下方法:

android:requestLegacyExternalStorage="true"

答案 2 :(得分:0)

用于在 Android 11 上进行测试的快速但非永久性修复:

添加android:requestLegacyExternalStorage="true"

和改变

compileSdkVersion 29 targetSdkVersion 29

都到 build.gradle 中的 29

相关问题