【发布时间】:2013-12-19 21:51:27
【问题描述】:
我想从 url 加载巨大的图像并在我的设备上显示。我为此阅读了this 的帖子。
在该示例中,他们使用本机可绘制对象,但我想从服务器获取图像。
我正在使用此代码
private void getBitmap(ImageView imageView,final String url){
mBitmapOptions = new BitmapFactory.Options();
mBitmapOptions.inJustDecodeBounds = true;
URL mUrl = null;
InputStream is= null;
try {
mUrl = new URL(url);
is = mUrl.openStream();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// BitmapFactory.decodeStream(is,null,mBitmapOptions);
mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth,
mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
mBitmapOptions.inJustDecodeBounds = false;
mBitmapOptions.inBitmap = mCurrentBitmap;
mBitmapOptions.inSampleSize = 1;
BitmapFactory.decodeStream(is,null,mBitmapOptions);
imageView.setImageBitmap(mCurrentBitmap);
// Bitmap croppedBitmap=Bitmap.createBitmap(Bitmap.createBitmap(mCurrentBitmap, 0, mCurrentBitmap.getHeight()/2, mCurrentBitmap.getWidth(), mCurrentBitmap.getHeight()/2));
// imageView.setImageBitmap(croppedBitmap);
}
我想在这里从 url 获取图像并调整它的大小,但我有一个例外。
java.lang.IllegalArgumentException: width and height must be > 0
我在这里做错了什么?或者你能建议我更好的答案吗?
【问题讨论】:
-
谢谢@Damian,但毕加索会自动处理大图像加载,还是我应该自己处理?
-
您评论的
BitmapFactory.decodeStream(is,null,mBitmapOptions);是在这里实际填充位图选项中的宽度和高度。您最好先下载文件,然后对文件执行此操作。 -
@njzk2 有几张图片的尺寸很大,我认为在 perferm 操作后下载所有图片并不是最好的方法。
-
无论如何你都必须下载它们,你知道的。因为如果不下载它们,你就无法知道它们的大小或 b/ 显示它们
-
@pmb 你得到异常是因为
// BitmapFactory.decodeStream(is,null,mBitmapOptions);在你尝试解码时没有设置 mBitmapOptions。查看我之前的回答,尝试解决您的完整解码问题。
标签: android bitmap android-imageview android-image bitmapimage