从assset获取文件

因为assets文件夹内文件不能直接获取绝对路径,所以当需要从其中获取特定文件路径的时候,可以将文件copy到能获取路径的地方。

AssetManager am = this.getAssets();
String sdPath = Environment.getExternalStorageDirectory().getPath();
try {
    InputStream is = am.open("x5.tbs.apk");
    FileOutputStream fos = new FileOutputStream(sdPath + "/x5.tbs.apk");
    byte[] buff = new byte[512];
    int count = is.read(buff);
    while (count != -1) {
        fos.write(buff);
        count = is.read(buff);
    }
    is.close();
    fos.close();
} catch (IOException e) {
    e.printStackTrace();
}
File file = new File(sdPath + "/x5.tbs.apk");
if (file.exists()) {
    Log.i(TAG, "onCreate: fileExists");
} else {
    Log.i(TAG, "onCreate: fileNotExists");
}

 

posted @ 2022-06-08 16:21  北海南竹  阅读(105)  评论(0)    收藏  举报