我的应用程序允许用户将其数据导出给其他用户,或仅保存为备份。
导入/导出工作正常
为了让用户在首次安装我的应用程序时获得示例数据,我想打包一些默认数据。我创建了示例数据,测试了“工作精细”,然后将其打包在assets
文件夹中,并在用户首次运行应用程序时加载。
但是我发现文件未找到异常
代码在这里:
private List<Giveaway> loadJsonData(Uri data, User user) {
List<Giveaway> result = null;
try {
InputStream is = this.getContentResolver().openInputStream(data);
Gson parser = new GsonBuilder().setDateFormat("dd/MM/yy").setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setLongSerializationPolicy(LongSerializationPolicy.DEFAULT).setLenient().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.TRANSIENT).create();
Set<Giveaway> temp = new HashSet<Giveaway>(50);
temp.addAll((Collection<? extends Giveaway>) parser.fromJson(new InputStreamReader(is), TypeToken.getParameterized(List.class, Giveaway.class).getType()));
result = new ArrayList<Giveaway>(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
result = new ArrayList<Giveaway>(1);
}
return result;
}
我用它来称呼它
loadJsonData(Uri.parse("file:///android_asset/giveaway_export.json"), sampleUser);
答案 0 :(得分:1)
使用AssetManager这是一个示例:
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("giveaway_export.json");
} catch (IOException e) {
e.printStackTrace();
}
所以您必须更改方法:
private List<Giveaway> loadJsonData(Uri data, User user) {
List<Giveaway> result = null;
try {
//InputStream is = this.getContentResolver().openInputStream(data);
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("giveaway_export.json");
} catch (IOException e) {
e.printStackTrace();
}
Gson parser = new GsonBuilder().setDateFormat("dd/MM/yy").setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).setLongSerializationPolicy(LongSerializationPolicy.DEFAULT).setLenient().excludeFieldsWithModifiers(Modifier.FINAL, Modifier.STATIC, Modifier.TRANSIENT).create();
Set<Giveaway> temp = new HashSet<Giveaway>(50);
temp.addAll((Collection<? extends Giveaway>) parser.fromJson(new InputStreamReader(is), TypeToken.getParameterized(List.class, Giveaway.class).getType()));
result = new ArrayList<Giveaway>(temp);
} catch (FileNotFoundException e) {
e.printStackTrace();
result = new ArrayList<Giveaway>(1);
}
return result;
}
请记住,如果您使用的是Android 6.0+,则需要声明权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
并需要手动权限:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Verify permission for Android 6.0+
checkExternalStoragePermission();
}
使用此方法:
private void checkExternalStoragePermission() {
int permissionCheck = ContextCompat.checkSelfPermission(
this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
Log.i("Message", "You require permissions!.");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 225);
} else {
Log.i("Message", "you have already permissions!");
}
}
答案 1 :(得分:1)
file:///android_asset
适用于WebView
,几乎没有其他功能。使用AssetManager
处理资产-通过在getAssets()
上调用Context
,例如Activity
,来获得资产之一。