将 res 资源文件转换成 file

来源:互联网 发布:hbuilder for mac下载 编辑:程序博客网 时间:2024/06/10 08:52

将资源文件转换成本地文件:

读取到流中,转换成bitmap,再从bitmap转变成file


InputStream is = getResources().openRawResource(R.drawable.icon);


        Bitmap bitmap = BitmapFactory.decodeStream(is);

        String defaultPath = getApplicationContext().getFilesDir()
                .getAbsolutePath() + "/defaultGoodInfo";
        File file = new File(defaultPath);
        if (!file.exists()) {
            file.mkdirs();
        } else {
            return;
        }
        String defaultImgPath = defaultPath + "/messageImg.jpg";
        file = new File(defaultImgPath);
        try {
            file.createNewFile();

            FileOutputStream fOut = new FileOutputStream(file);

            bitmap.compress(Bitmap.CompressFormat.PNG, 20, fOut);
            is.close();
            fOut.flush();
            fOut.close();
        } catch (Exception e) {
            e.printStackTrace();

        }



0 0