如何将android Path字符串转换为Assets文件夹中的文件?

Nul*_*ion 62 android assets android-assets

我需要知道assets文件夹上文件的字符串路径,因为我使用的是需要接收字符串路径的map API,我的地图必须存储在assets文件夹中

这是我正在尝试的代码:

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

出现问题"file:///android_asset/m1.map"因为没有加载地图.

哪个是存储在我的assets文件夹中的文件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);}
Run Code Online (Sandbox Code Playgroud)

Jac*_*alk 89

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());
Run Code Online (Sandbox Code Playgroud)

  • 我得到异常:java.io.FileNotFoundException:m1.map (4认同)
  • 好的,这是完全正确的,资产目录中的FILES被压缩,所以你必须提取到一个字节数组,写入临时文件并加载该文件,谢谢! (2认同)

Jac*_*cki 10

您可以使用此方法.

    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;
    }
Run Code Online (Sandbox Code Playgroud)

在这种情况下,您永远不应该使用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) } }
    }
Run Code Online (Sandbox Code Playgroud)


Dum*_*tov 9

请查看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);
    }
Run Code Online (Sandbox Code Playgroud)


小智 5

只是添加 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
}
Run Code Online (Sandbox Code Playgroud)