英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论