从Android 10的资产管理器获取数据/JSON/文件时出现问题。

huangapple go评论62阅读模式
英文:

Issues when fetching data/Json/file from assets manager on Android 10

问题

以下是读取来自资产的JSON文件的代码,它在除了Android版本为10的Pixel 3 XL设备之外的所有其他设备上都有效。此设备从资产返回null值。

StringBuilder builder = new StringBuilder();
BufferedReader reader = null;
try {
    reader = new BufferedReader(new InputStreamReader(MyApp.getAppInstance().getAssets().open(fileName)));
    // 进行读取,通常循环直到文件末尾
    String mLine;
    builder = new StringBuilder();
    while ((mLine = reader.readLine()) != null) {
        builder.append(mLine);
    }
} catch (IOException e) {
    // 记录异常
} finally {
    if (reader != null) {
        try {
            reader.close();
        } catch (IOException e) {}
    }
}
return builder.toString();
英文:

Below is my code for reading Json file from assets, It works on every other device except Pixel 3 XL which android version is 10.This device returning null from assets

StringBuilder builder = new StringBuilder();
    BufferedReader reader = null;
    try {
        reader = new BufferedReader( newInputStreamReader( MyApp.getAppInstance().getAssets().open(fileName)));
        // do reading, usually loop until end of file reading
        String mLine;
        builder = new StringBuilder();
        while ((mLine = reader.readLine()) != null) {
            builder.append(mLine);
        }
    } catch (IOException e) {
      //log the exception
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {}
        }
    }
    return builder.toString();
}

答案1

得分: 1

确保您已在清单中提供所需的权限


对于Android 10问题,请尝试在应用程序标记中使用

android:requestLegacyExternalStorage="true"

英文:

Make sure you have given the required permission in manifest

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

and for Android 10 issue try using

android:requestLegacyExternalStorage="true"

inside your application tag

huangapple
  • 本文由 发表于 2020年1月3日 17:47:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/59576288.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定