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

时间:2014-09-08 09:03:48

标签: android image bitmap drawable bitmapfactory

我想将图像从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复制到图库:它在输入流上提供文件未找到的异常。 String 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();
            }
        }
    }

0 个答案:

没有答案