从android中的URL限制内存中抽取

时间:2012-11-16 10:35:47

标签: java android memory

我想从URL获取图片并将其转换为drawable。我有这种方法,工作正常:

public static Drawable getDrawableFromUrl(String url) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream input = connection.getInputStream();

    x = BitmapFactory.decodeStream(input);
    return new BitmapDrawable(x);
}

除了一件事。当图片太大时,应用程序崩溃。

E/dalvikvm-heap(12750): 13142360-byte external allocation too large for this process.
E/dalvikvm(12750): Out of memory: Heap Size=6023KB, Allocated=3177KB, Bitmap Size=2563KB, Limit=20480KB
E/dalvikvm(12750): Trim info: Footprint=6023KB, Allowed Footprint=6023KB, Trimmed=952KB
E/GraphicsJNI(12750): VM won't let us allocate 13142360 bytes

我可以以某种方式增加图像所需的内存吗?或者也许有办法绕过这个问题?

4 个答案:

答案 0 :(得分:2)

Loading Large Bitmaps Efficiently

上面的链接从url返回重新调整大小的位图,然后你可以转换为bitmap drawable。

从该链接下载并使用Bitmap fun示例应用程序。

答案 1 :(得分:1)

您需要按照此链接建议缩小图像或调整其大小

Loading Large Image

以下是一些代码段:

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int resId,
        int reqWidth, int reqHeight) {

    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(res, resId, options);
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(res, resId, options);
}

public static int calculateInSampleSize(
        BitmapFactory.Options options, int reqWidth, int reqHeight) {
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {
    if (width > height) {
        inSampleSize = Math.round((float)height / (float)reqHeight);
    } else {
        inSampleSize = Math.round((float)width / (float)reqWidth);
    }
}
return inSampleSize;
}

在设置drawable

时调用此部件
MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mydrawable, 420, 620));

答案 2 :(得分:1)

确定。这是最终有效的代码。 正如所建议的那样,我使用给定的函数减小了图像的大小:calculateInSampleSize - thx Androyds。

public static Drawable getDrawableFromUrl(String url, Context context) throws IOException {
    Bitmap x;

    HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
    connection.connect();
    InputStream inputStream = connection.getInputStream();
    BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

    BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
bufferedInputStream.mark(connection.getContentLength());
BitmapFactory.decodeStream(bufferedInputStream, null, options);
bufferedInputStream.reset();

options.inJustDecodeBounds = false;
options.inSampleSize = calculateInSampleSize(options, 400, 400);

x = BitmapFactory.decodeStream(bufferedInputStream, null, options);

bufferedInputStream.close();
connection.disconnect();

    BitmapDrawable y = new BitmapDrawable(x);

    return y;
}

答案 3 :(得分:0)

我建议您将文件临时保存在手机上并阅读并将其用作可绘图文件。但是,如果手机无法加载图像,则需要缩小发送者或打开图像之前的图像。