位图未保存在SD卡中

时间:2014-07-25 08:21:01

标签: android bitmap

已更新

我正在尝试将位图保存在SD卡中。代码正在运行,没有显示任何错误和异常,但图像未保存在库中。请帮助

日志报告

07-25 04:26:04.126: D/dalvikvm(2453): GC_FOR_ALLOC freed 82K, 7% free 2788K/2976K, paused 210ms, total 228ms
07-25 04:26:04.168: I/dalvikvm-heap(2453): Grow heap (frag case) to 3.894MB for 1127536-byte allocation
07-25 04:26:04.306: D/dalvikvm(2453): GC_FOR_ALLOC freed 2K, 5% free 3887K/4080K, paused 137ms, total 137ms
07-25 04:26:04.375: E/MainActivity(2453): Error in creating fragment
07-25 04:26:04.706: I/Choreographer(2453): Skipped 30 frames!  The application may be doing too much work on its main thread.
07-25 04:26:04.735: D/gralloc_goldfish(2453): Emulator without GPU emulation detected.
07-25 04:26:09.326: D/dalvikvm(2453): GC_FOR_ALLOC freed 114K, 5% free 4285K/4500K, paused 29ms, total 35ms
07-25 04:26:12.026: D/dalvikvm(2453): GC_FOR_ALLOC freed 52K, 4% free 4426K/4580K, paused 38ms, total 44ms
07-25 04:26:12.036: I/dalvikvm-heap(2453): Grow heap (frag case) to 5.531MB for 1166416-byte allocation
07-25 04:26:12.107: D/dalvikvm(2453): GC_FOR_ALLOC freed 2K, 3% free 5563K/5720K, paused 69ms, total 69ms
07-25 04:26:12.266: D/dalvikvm(2453): GC_FOR_ALLOC freed <1K, 3% free 5563K/5720K, paused 29ms, total 29ms
07-25 04:26:12.276: I/dalvikvm-heap(2453): Grow heap (frag case) to 6.641MB for 1166416-byte allocation
07-25 04:26:12.316: D/dalvikvm(2453): GC_FOR_ALLOC freed <1K, 3% free 6702K/6860K, paused 36ms, total 36ms

在setContentView之后的MainActivity中我正在这样做,

// Find the SD Card path
filepath = Environment.getExternalStorageDirectory();

我调用此函数来保存图像

public void saveImage(Bitmap bitmap) {

    OutputStream output;

    // Create a new folder in SD Card
    File dir = new File(filepath.getAbsolutePath()
            + "/Save Image Tutorial/");
    dir.mkdirs();

    String fName = "ige" + ".png";

    // Create a name for the saved image
    File file = new File(dir, fName);

    try {

        output = new FileOutputStream(file);

        // Compress into png format image from 0% - 100%
        bitmap.compress(Bitmap.CompressFormat.PNG, 100, output);
        output.flush();
        output.close();

        // Show a toast message on successful save
        Toast.makeText(MainActivity.this, "Image Saved to SD Card",
                Toast.LENGTH_SHORT).show();
    } catch (Exception e) {
        e.printStackTrace();
        // Show a toast message on successful save
        Toast.makeText(MainActivity.this, "Save failed", Toast.LENGTH_SHORT)
                .show();
    }
}

1 个答案:

答案 0 :(得分:1)

保存后需要更新图库; 喜欢这样:

getContext().sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+    Environment.getExternalStorageDirectory())));