【问题标题】:Bitmap is not saving in SD Card位图未保存在 SD 卡中
【发布时间】:2014-07-25 08:21:01
【问题描述】:

更新

我正在尝试将位图保存在 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();
    }
}

【问题讨论】:

  • 你在androidmanifest中添加了权限吗?
  • 我猜你的文件路径在 logcat 中是错误的 ..and update question to full code
  • 我已经更新了问题

标签: android bitmap


【解决方案1】:

您需要在保存后更新图库; 像这样:

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

【讨论】:

    猜你喜欢
    • 2012-10-21
    • 2012-08-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多