2014-07-25 74 views
-3

修訂位圖不是在SD卡保存

我想保存在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 

在MainActivity我這樣做的setContentView後,

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

沒有ü在androidmanifest添加權限? – KOTIOS

+0

是的。我已經添加了它 – user3864752

+0

張貼您的日誌貓 –

回答

1

您需要更新保存後的畫廊; 像這樣:

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