如何获取资产文件夹中文件的android路径字符串?
我需要知道资产文件夹中文件的字符串路径,因为我使用的地图API需要接收字符串路径,并且我的地图必须存储在资产文件夹中
这是我正在尝试的代码:
MapView mapView = new MapView(this);
mapView.setClickable(true);
mapView.setBuiltInZoomControls(true);
mapView.setMapFile("file:///android_asset/m1.map");
setContentView(mapView);
is.read(buffer);发生了问题,因为未加载地图。
资产文件夹中存储的m1.map文件的正确字符串路径文件是哪个?
谢谢
编辑Dimitru:此代码无效,它在is.read(buffer);上失败,并带有IOException
try {
InputStream is = getAssets().open("m1.map");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
text = new String(buffer);
} catch (IOException e) {throw new RuntimeException(e);}
4个解决方案
87 votes
AFAIK资产目录中的文件不会解压缩。而是直接从APK(ZIP)文件中读取它们。
因此,您真的无法制作出期望文件接受资产“文件”的内容。
相反,您必须提取资产并将其写入一个单独的文件,如Dumitru建议的那样:
File f = new File(getCacheDir()+"/m1.map");
if (!f.exists()) try {
InputStream is = getAssets().open("m1.map");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }
mapView.setMapFile(f.getPath());
Jacob Nordfalk answered 2019-10-12T02:15:24Z
9 votes
查看SDK随附的API示例中的ReadAsset.java。
try {
InputStream is = getAssets().open("read_asset.txt");
// We guarantee that the available method returns the total
// size of the asset... of course, this does mean that a single
// asset can't be more than 2 gigs.
int size = is.available();
// Read the entire asset into a local byte buffer.
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
// Convert the buffer into a string.
String text = new String(buffer);
// Finally stick the string into the text view.
TextView tv = (TextView)findViewById(R.id.text);
tv.setText(text);
} catch (IOException e) {
// Should never happen!
throw new RuntimeException(e);
}
Dumitru Hristov answered 2019-10-12T02:15:48Z
7 votes
您可以使用此方法。
public static File getRobotCacheFile(Context context) throws IOException {
File cacheFile = new File(context.getCacheDir(), "robot.png");
try {
InputStream inputStream = context.getAssets().open("robot.png");
try {
FileOutputStream outputStream = new FileOutputStream(cacheFile);
try {
byte[] buf = new byte[1024];
int len;
while ((len = inputStream.read(buf)) > 0) {
outputStream.write(buf, 0, len);
}
} finally {
outputStream.close();
}
} finally {
inputStream.close();
}
} catch (IOException e) {
throw new IOException("Could not open robot png", e);
}
return cacheFile;
}
在这种情况下,切勿使用InputStream.available()。 它仅返回已缓冲的字节。 带有.available()的方法将永远无法使用较大的文件,并且根本无法在某些设备上使用。
在Kotlin(; D):
@Throws(IOException::class)
fun getRobotCacheFile(context: Context): File = File(context.cacheDir, "robot.png")
.also {
it.outputStream().use { cache -> context.assets.open("robot.png").use { it.copyTo(cache) } }
}
Jacek Marchwicki answered 2019-10-12T02:16:25Z
3 votes
只是添加了Jacek的完美解决方案。 如果您尝试在Kotlin中进行此操作,则不会立即起作用。 相反,您将需要使用以下代码:
@Throws(IOException::class)
fun getSplashVideo(context: Context): File {
val cacheFile = File(context.cacheDir, "splash_video")
try {
val inputStream = context.assets.open("splash_video")
val outputStream = FileOutputStream(cacheFile)
try {
inputStream.copyTo(outputStream)
} finally {
inputStream.close()
outputStream.close()
}
} catch (e: IOException) {
throw IOException("Could not open splash_video", e)
}
return cacheFile
}
CheesusCrust answered 2019-10-12T02:16:49Z