【问题标题】:Out of Memory Error - Bitmap Size内存不足错误 - 位图大小
【发布时间】:2012-11-06 19:42:12
【问题描述】:

我想显示此图像并将其上传到服务器。

我正在使用相机应用拍摄照片并将照片的文件路径返回给活动。我在某些手机上遇到内存不足错误,因为我尝试在设备之间导入恒定的图像大小。

在手机内存限制的情况下,我怎样才能将最大的图片上传到服务器?

代码如下:

请求加载 Aync

GetBitmapTask GBT = new GetBitmapTask(dataType, path, 1920, 1920, loader);
GBT.addAsyncTaskListener(new AsyncTaskDone() 
{
    @Override
    public void loaded(Object resp) 
    {               
        crop.setImageBitmap((Bitmap)resp);
        crop.setScaleType(ScaleType.MATRIX);
    }

    @Override
    public void error() {
    }
});

GBT.execute();

引发OOM错误的异步任务

public class GetBitmapTask extends AsyncTask<Void, Integer, Bitmap> 
{

 ...

@Override
public Bitmap doInBackground(Void... params) 
{
    Bitmap r = null;

    if (_dataType.equals("Unkown"))
    {
        Logger.e(getClass().getName(), "Error: Unkown File Type");
        return null;
    }
    else if (_dataType.equals("File")) 
    {   
        Options options = new Options();            
        options.inJustDecodeBounds = true;

        //Logger.i(getClass().getSimpleName(), _path.substring(7));

        BitmapFactory.decodeFile(_path.substring(7), options);

        options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

        Logger.i(getClass().getSimpleName(), 
                "height: " + options.outHeight +
                "\nwidth: " + options.outWidth +
                "\nmimetype: " + options.outMimeType +
                "\nsample size: " + options.inSampleSize);

        options.inJustDecodeBounds = false;
        r = BitmapFactory.decodeFile(_path.substring(7), options);

    } 
    else if (_dataType.equals("Http")) 
    {
        r = _loader.downloadBitmap(_path, reqHeight);

        Logger.i(getClass().getSimpleName(), "height: " + r.getHeight() +
                                            "\nwidth: " + r.getWidth());
    }

    return r;
}

public static int calculateInSampleSize( Options options, int reqWidth, int reqHeight) 
{
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    while (height / inSampleSize > reqHeight || width / inSampleSize > reqWidth) 
    {
        if (height > width)
        {
            inSampleSize = height / reqHeight;
            if (((double)height % (double)reqHeight) != 0) 
            {
                inSampleSize++;
            }
        } 
        else 
        {
            inSampleSize = width / reqWidth;
            if (((double)width % (double)reqWidth) != 0) 
            {
                inSampleSize++;
            }
        }
    }
    return inSampleSize;
}
}

【问题讨论】:

    标签: android


    【解决方案1】:

    您可以为相机提供指向要将图像保存到的文件的 uri:

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, mDefaultPhotoUri); // set path to image file
    

    然后您可以将该文件上传到您的服务器,这样您就有了完整的位图大小。另一方面,没有必要(在许多设备上也没有办法)在 UI 中解码和显示 1920x1920(或类似的)位图,它太大了。

    希望这会有所帮助。

    【讨论】:

    • 我最终发现错误在代码中比位图任务更靠后,但你说得对,我不需要显示 1920X1920 图像。谢谢!
    猜你喜欢
    • 2011-09-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-27
    相关资源
    最近更新 更多