我尝试从资产文件夹中的现有文件加载,而不是像下面的代码那样从SD卡加载:
MapDataStore mapDataStore = new MapFile(
new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "berlin.map"));我不确定如何在Android中做到这一点,并寻求帮助。
发布于 2017-03-21 06:27:28
如果我没记错的话,您不能访问assets文件夹中的.map文件。你必须把它从那里复制到SD卡上。
private void provideMapData() {
String sourceFile = res.getString(R.array.mapsource);
String destinationFile = res.getString(R.array.mapdestination);
String pathPrefix = activity.getExternalFilesDir(null) + "/";
File directory = new File(pathPrefix);
if (directory.exists() | directory.mkdirs()) {
AssetManager assetManager = activity.getAssets();
InputStream inputStream;
OutputStream outputStream;
File file = new File(pathPrefix + destinationFile);
if (!file.exists()) {
try {
inputStream = assetManager.open(sourceFile);
outputStream = new FileOutputStream(file);
byte[] buffer = new byte[8192];
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
inputStream.close();
outputStream.flush();
outputStream.close();
} catch (IOException iOE) {
Log.e("Error: ", "provideMapData()");
}
}
}
}然后,我像这样加载MapDataStore:
MapDataStore mapDataStore = new MapFile(new File(activity.getExternalFilesDir(null) + "/" + destinationFile));根据this的说法,您应该将以下内容放入清单文件中:
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18" />它将使您不必显式地要求用户授予WRITE_EXTERNAL_STORAGE权限。
https://stackoverflow.com/questions/42865909
复制相似问题