将图像从res/drawable保存到图像库Android

将图像从res/drawable保存到图像库Android,android,image,bitmap,drawable,bitmapfactory,Android,Image,Bitmap,Drawable,Bitmapfactory,我想将图像从res/drawable保存到图像库。我正在使用以下代码,但它什么也没做。 我的代码有什么问题String Drawable表示可绘制文件夹中的图像名称 File direct = new File(Environment.getExternalStorageDirectory() + "/Images"); if (!direct.exists()) { direct.mkdirs();

我想将图像从res/drawable保存到图像库。我正在使用以下代码,但它什么也没做。

我的代码有什么问题
String Drawable
表示可绘制文件夹中的图像名称

        File direct = new File(Environment.getExternalStorageDirectory()
                + "/Images");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        ByteArrayOutputStream bos = null;
        FileOutputStream fos = null;

        try {
            Bitmap bitmap = BitmapFactory.decodeResource(
                    context.getResources(),
                    context.getResources().getIdentifier(
                            "@drawable/" + Drawable, "drawable",
                            context.getPackageName()));

            bos = new ByteArrayOutputStream();
            bitmap.compress(CompressFormat.JPEG, 95, bos);

            byte[] bitmapdata = bos.toByteArray();
            fos = new FileOutputStream(direct + "/" + "IMG-" + CurrentDateTime
                    + ".jpg");
            fos.write(bitmapdata);
        } catch (Exception e) {
            Log.e("Internal Image Save Error->", e.toString());
        } finally {
            try {
                if (bos != null) {
                    bos.close();
                }
                if (fos != null) {
                    fos.close();
                    fos.flush();
                }
            } catch (IOException ignored) {
                Log.e("Internal Image Save Error->", ignored.toString());
            }
        }

我刚刚发现它正在保存图像,但需要10分钟左右的时间。

将图像从Drawable复制到Gallery:在输入流中出现“未找到文件”异常。字符串Drawable是图像名称,即data1

public static void downloadInternalImage(String Drawable, Context context) {

        Toast.makeText(context, "Downloading Image...\nPlease Wait.",
                Toast.LENGTH_LONG).show();

        File direct = new File(Environment.getExternalStorageDirectory()
                + "/Images");

        if (!direct.exists()) {
            direct.mkdirs();
        }

        InputStream input = null;
        OutputStream output = null;

        try {
            input = new FileInputStream("android.resource://"
                    + context.getPackageName() + "/drawable/" + Drawable + "");
            output = new FileOutputStream(direct + "/" + "IMG-"
                    + CurrentDateTime + ".jpg");

            byte[] buf = new byte[1024];
            int len;
            while ((len = input.read(buf)) > 0) {
                output.write(buf, 0, len);
            }

            Toast.makeText(context, "Image Saved.", Toast.LENGTH_LONG).show();
        } catch (Exception e) {
            Log.e("Internal Image Save Error->", e.toString());

            Toast.makeText(context,
                    "Couldn't Save Image.\nError:" + e.toString() + "",
                    Toast.LENGTH_LONG).show();
        } finally {
            try {
                if (input != null) {
                    input.close();
                }
                if (output != null) {
                    output.close();
                }
            } catch (IOException ignored) {
                Log.e("Internal Image Save Error->", ignored.toString());

                Toast.makeText(
                        context,
                        "Couldn't Save Image.\nError:" + ignored.toString()
                                + "", Toast.LENGTH_LONG).show();
            }
        }
    }

10分钟?它的尺寸是多少?保存时会发生什么?为什么要使用BitmapFactory?它会改变你的照片。您可以使用inputstream和fileoutputstream复制“文件”。
String Drawable代表图像名称。这不是一个好主意,因为Drawable已经是Android中的一个类了。最好使用“imageName”。它的大小几乎不在10-30kbi之间。检查了一些示例,所有示例都使用
BitMapFactory
。是的。可以是。但你为什么要效仿坏榜样呢?你知道BitmapFactory在做什么吗?你只是想复制一个文件,不是吗?如果是这样的话,这就太过分了,而且文件与您放在drawable文件夹中的文件不同。但最好把注意力集中在10分钟上。