【问题标题】:android - add images from drawable folder into a created file directoryandroid - 将drawable文件夹中的图像添加到创建的文件目录中
【发布时间】:2015-05-29 07:41:58
【问题描述】:

我希望能够获取存储在我的应用程序可绘制文件夹中的 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");

    }

}

谢谢

【问题讨论】:

  • 请显示您尝试过的代码,以及您遇到的错误
  • 这些可绘制对象是在应用程序中使用,还是您只想将位图放到外部目录中?

标签: android file directory android-file


【解决方案1】:

解决了,我是这样解决的:

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();
                }

【讨论】:

    猜你喜欢
    • 2015-05-16
    • 2015-11-15
    • 2011-10-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多