如何获取Android Pathstring到资产文件夹上的文件?

我需要知道资产文件夹上的文件的stringpath ,因为我使用的地图api需要接收一个stringpath,我的地图必须存储在资产文件夹

这是我正在尝试的代码:

MapView mapView = new MapView(this); mapView.setClickable(true); mapView.setBuiltInZoomControls(true); mapView.setMapFile("file:///android_asset/m1.map"); setContentView(mapView); 

"file:///android_asset/m1.map"有些问题,因为地图没有被加载。

这是正确的stringpath文件存储在我的资产文件夹文件m1.map?

谢谢

编辑Dimitru:此代码不起作用,它在is.read(buffer);失败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);} 

AFAIK资产目录中的文件不会被解压缩。 而是直接从APK(ZIP)文件中读取。

所以,你真的不能做一个文件接受一个资产'文件'。

相反,您必须提取资产并将其写入单独的文件,就像Dumitru所build议的那样:

  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()); 

查看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); } 

你可以使用这个方法。

  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()的方法将永远不能处理更大的文件,并且根本不能在某些设备上工作。

只是为了增加Jacek的完美解决scheme。 如果你想在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 }