How to get android path string to a file in the Assets folder?

I need to know the path of the line to the file in the resource folder, because I use the api map, which should receive the string path, and my maps should be stored in the resource folder

This is the code I'm trying:

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

Something goes wrong with "file:///android_asset/m1.map" because the map is not loading.

What is the correct line path file to the m1.map file stored in my resource folder?

thank

EDIT for Dimitru: this code does not work, it fails on is.read(buffer); with an 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);} 
+46
android android-assets assets
Dec 12 '11 at 13:09
source share
4 answers

AFAIK files in the resource directory are not decompressed. Instead, they are read directly from the APK (ZIP) file.

So, you really cannot create material that expects the file to accept the asset file.

Instead you will need to extract the asset and write it to a separate file, for example, Dumitru suggests:

  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()); 
+69
Dec 12 '11 at 13:33
source share

Take a look at the ReadAsset.java sample APIs that come with the SDK.

  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); } 
+7
Dec 12 '11 at 13:18
source share

You can use this method.

  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; } 

You should never use InputStream.available () in such cases. It returns only bytes that are buffered. The method with .available () will never work with large files and will never work on some devices.

+5
Nov 04 '13 at
source share

Just add the perfect Jacek solution. If you try to do this in Kotlin, this will not work right away. Instead, you will want to use this:

 @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 } 
+1
Jun 29 '17 at 15:55
source share



All Articles