Android 分块保存位图图像

Android 分块保存位图图像,android,android-sdcard,save-image,Android,Android Sdcard,Save Image,我想将位图图像保存在sd卡中,我可以保存它,但有一段时间我的活动因为内存不足而停止 所以我可以将图像分块保存,而不是以字节数组的形式保存 我的代码如下: try { ByteArrayOutputStream bytes = new ByteArrayOutputStream(); b.compress(Bitmap.CompressFormat.JPEG, 40, bytes); File f = new File(Environme

我想将位图图像保存在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();
    }

解决此问题的最佳方法是将图像的大小缩小到所需的视图大小,并在后台线程上执行所有繁重的工作(异步任务),当后台线程工作时,可以显示任何虚拟图像表单资源,并且在正确处理图像后,用上一个替换位图

读过这篇文章后,再继续


要减少此问题,您可以做的一件事是调整图像的大小或重新缩放图像,然后将其保存到内存中

下面是有帮助的代码。你可以试试下面的方法

请按以下方式使用上述各项:


if(文件大小是否显示任何错误如果是,请发布您的日志。不,Grishu,它只是杀死活动并在活动堆栈中停止活动。我在具有更多ram的设备中尝试过此方法,在其中工作正常,因此我得出结论,这是内存问题。请检查我的答案并尝试使用它。我确信这将对您有所帮助。我已经缩放了im年龄:(查看我的更新答案。Grishu,什么是文件大小?在编写文件时,文件大小是0,对吗?
filesize
是您拥有的文件。
  // 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;
}
 /**
 * 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.
         }