android - 将drawable文件夹中的图像添加到创建的文件目录中

时间:2015-03-25 14:50:56

标签: android file directory android-file

我希望能够拍摄我存储在应用程序可绘制文件夹中的png图像(称为1.png,2.png,3.png),并将它们复制/保存到由启动时我的应用。
我尝试了.createnewfile()但无法正常工作。

用于创建我的文件目录的代码:

//-----create file directory for images
public void createDirectory() {

    try {

        File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES");

        if (!imageDirectory.exists()) {

            imageDirectory.mkdirs();

            //something here to take images from drawable folder and place them into my newely created directory

            File imageFile = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES", //something here getDrawable maybe?
                    );
            imageFile.createNewFile();


        }

    }

    catch (Exception e) {

        Log.d("MAKE_DIR", "error creating directory");

    }

}

感谢

1 个答案:

答案 0 :(得分:0)

解决了,我就这样做了:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.maptile_1);

                ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

                File f = new File(Environment.getExternalStorageDirectory() + "/DCIM/C2AT_IMAGES"
                        + File.separator + "test.jpg");
                try {
                    f.createNewFile();
                    FileOutputStream fo = new FileOutputStream(f);
                    fo.write(bytes.toByteArray());
                    fo.close();
                } catch (IOException e) {
                    e.printStackTrace();

                    toastMsg = e.toString();
                    toast();
                }