0

我想从 URL 获取图像并将其转换为可绘制的。我有这个方法,效果很好:

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

4 回答 4

2

有效加载大型位图

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

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

于 2012-11-16T10:49:17.783 回答
1

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

加载大图像

以下是一些代码片段:

    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;
}

设置可绘制对象时只需调用此部分

MyImageview.setImageBitmap(decodeSampledBitmapFromResource(getResources(), mydrawable, 420, 620));
于 2012-11-16T11:01:52.743 回答
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;
}
于 2012-11-19T11:06:31.010 回答
0

我建议您将文件临时保存在手机上并阅读它并将其用作可绘制对象。但如果手机无法加载图片,则需要从发件人处或在打开之前将图片按比例缩小。

于 2012-11-16T10:45:05.140 回答