2013-03-22 77 views
1

我想保存在SD卡中的位图图像,我能够保存它,但有一段时间我的活动因为低内存死亡。保存大块位图图像

所以我可以保存图像块,而不是保存在字节数组的形式。

我的代码如下所示:

try { 
     ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 

     b.compress(Bitmap.CompressFormat.JPEG, 40, bytes); 

     File f = new File(Environment.getExternalStorageDirectory() + File.separator + "temp.jpg"); 
     if (f.exists()) { 
      f.delete(); 
     } 
     f.createNewFile(); 

     FileOutputStream fo = new FileOutputStream(f); 
     fo.write(bytes.toByteArray()); 
     fo.flush(); 
     fo.close(); 

    } catch (Exception e) { 

     e.printStackTrace(); 
    } 
+0

是否显示任何错误,如果是的话,请发表您的logcat的。 – GrIsHu 2013-03-22 04:16:45

+0

没有Grishu,它只是杀死活动,并在活动堆栈中下来活动。我已经在更多内存的设备上尝试过这种方式,它工作正常,所以我得出结论认为它是内存问题。 – Dhrupal 2013-03-22 04:20:37

+0

结帐我的答案,并尝试使用它,当然它会帮助你。 – GrIsHu 2013-03-22 04:36:29

回答

1

为了减少这种问题,有一两件事你可以做的是调整重新调整之前图像,然后将其保存到内存中。

下面是将有助于的代码。你可以尝试下面的方法。

// decodes image and scales it to reduce memory consumption 
public static Bitmap decodeFile(File p_f) 
{ 
    try 
    { 
     // decode image size 
     BitmapFactory.Options m_opt = new BitmapFactory.Options(); 
     m_opt.inJustDecodeBounds = true; 
     BitmapFactory.decodeStream(new FileInputStream(p_f), null, m_opt); 
     // Find the correct scale value. It should be the power of 2. 
     final int REQUIRED_SIZE = 70; 
     int m_widthTmp = m_opt.outWidth, m_heightTmp = m_opt.outHeight; 
     int m_scale = 1; 
     while (true) 
     { 
      if (m_widthTmp/2 < REQUIRED_SIZE || m_heightTmp/2 < REQUIRED_SIZE) break; 
      m_widthTmp /= 2; 
      m_heightTmp /= 2; 
      m_scale *= 2; 
     } 
     // decode with inSampleSize 
     BitmapFactory.Options m_o2 = new BitmapFactory.Options(); 
     m_o2.inSampleSize = m_scale; 
     return BitmapFactory.decodeStream(new FileInputStream(p_f), null, m_o2); 
    } 
    catch (FileNotFoundException p_e) 
    { 
    } 
    return null; 
} 

编辑:

你也可以检查一个空间是否可用SD卡或不基于可用空间,您可以将图像保存到SD卡。我已经使用下面的方法来获得可用空间或不可用。

/** 
* This function find outs the free space for the given path. 
* 
* @return Bytes. Number of free space in bytes. 
*/ 
public static long getFreeSpace() 
{ 
    try 
    { 
     if (Environment.getExternalStorageDirectory() != null 
       && Environment.getExternalStorageDirectory().getPath() != null) 
     { 
      StatFs m_stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); 
      long m_blockSize = m_stat.getBlockSize(); 
      long m_availableBlocks = m_stat.getAvailableBlocks(); 
      return (m_availableBlocks * m_blockSize); 
     } 
     else 
     { 
      return 0; 
     } 
    } 
    catch (Exception e) 
    { 
     e.printStackTrace(); 
     return 0; 
    } 
} 

使用上述如下:

 if (fileSize <= getFreeSpace()) 
    { 
     //write your code to save the image into the sdcard. 
     } 
     else 
     { 
     //provide message that there is no more space available. 
     } 
+0

Grishu,什么是文件大小?在写文件大小的时候是0对吗? – Dhrupal 2013-03-22 08:36:33

+0

'filesize'是你的文件。 – GrIsHu 2013-03-22 09:43:01