[英]Android can't open file in assets folder
My app allows user to export its data to other users or just to save as backup. 我的应用程序允许用户将其数据导出到其他用户,或仅保存为备份。
The import/export is working FINE 导入/导出工作正常
In order to let the user have a sample data when it first installs my app I want to package some default data. 为了让用户在首次安装我的应用程序时获得示例数据,我想打包一些默认数据。 I created the sample data, tested IS WORKING FINE, then i packaged it in
assets
folder and load it when user runs the app for first time. 我创建了示例数据,测试了“精细工作”,然后将其打包在
assets
文件夹中,并在用户首次运行该应用程序时加载它。
But i'm getting file not found exception 但是我正在获取文件找不到异常
HERE GOES THE CODE: 这里是代码:
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;
}
and I call it using: 我用它来称呼它
loadJsonData(Uri.parse("file:///android_asset/giveaway_export.json"), sampleUser);
Use AssetManager this is an example: 使用AssetManager,这是一个示例:
AssetManager assetManager = getAssets();
InputStream is = null;
try {
is = assetManager.open("giveaway_export.json");
} catch (IOException e) {
e.printStackTrace();
}
so you have to change your method: 因此您必须更改方法:
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;
}
Remember if you are using android 6.0+ you need to declared the permission: 请记住,如果您使用的是Android 6.0+,则需要声明权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
and require manually permissions: 并需要手动权限:
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
//Verify permission for Android 6.0+
checkExternalStoragePermission();
}
use this method: 使用此方法:
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!");
}
}
file:///android_asset
works for WebView
and pretty much nothing else. file:///android_asset
适用于WebView
,几乎没有其他功能。 Use AssetManager
to work with assets — you get one of these by calling getAssets()
on a Context
, such as your Activity
. 使用
AssetManager
处理资产-您可以通过在诸如Activity
的Context
上调用getAssets()
来获得资产之一。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.