Android 获取assets的绝对路径

来源:互联网 发布:linux视频播放器安装 编辑:程序博客网 时间:2024/05/22 09:58


第一种方法:
       String path = "file:///android_asset/文件名";

第二种方法:
    InputStream abpath = getClass().getResourceAsStream("/assets/文件名");


若要想要转换成String类型

String path = new String(InputStreamToByte(abpath ));


    private byte[] InputStreamToByte(InputStream is) throws IOException {
        ByteArrayOutputStream bytestream = new ByteArrayOutputStream();
        int ch;
        while ((ch = is.read()) != -1) {
            bytestream.write(ch);
        }
        byte imgdata[] = bytestream.toByteArray();
        bytestream.close();
        return imgdata;
    }



鉴于有人搜到了博主这篇文章并且问了我一些东西,我想顺便在这里说一下。楼主说的第一种获取路径的方法,目前我所知道的,只是针对html的展示来使用的,比如你用到了webview,那么html的path可以用这种方法显示,其他情况不行。
第二种我没试过,问我的那人说没弄出来,于是搜了下,经试验貌似不行,有一个方法可以打印全部,但多出了三个。大家可以试验下。
private void Test() {
Resources resources = getResources();
AssetManager am = resources.getAssets();
try {
String[] files = am.list("");
for (String file : files) {
Log.i(TAG, file);
}
} catch (IOException e) {
e.printStackTrace();
}
}

0 0