我想从网址加载巨大的图片并在我的设备上显示。我这样做了this帖子。
在该示例中,他们使用原生drawable,但我想从服务器获取图像。
我正在使用此代码
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
我在这里做错了什么?或者你能建议我更好的回答吗?
答案 0 :(得分:4)
我会使用Picasso,那么你想要实现的就是这样简单:
Picasso.with(context).load(url).resize(50, 50).into(imageView)
答案 1 :(得分:0)
您因以下原因而获得例外:
// BitmapFactory.decodeStream(is,null,mBitmapOptions);
尝试调用时没有设置mBitmapOptions:
mCurrentBitmap = Bitmap.createBitmap(mBitmapOptions.outWidth, mBitmapOptions.outHeight, Bitmap.Config.ARGB_8888);
请仔细查看此问题的答案,以进一步优化您的代码:Getting the size of an image inputstream
答案 2 :(得分:0)
我根据http://developer.android.com/training/displaying-bitmaps/load-bitmap.html教程开发了一个解决方案。这是我的代码。棘手的部分包括使用字节数组来存储图像数据(因为不幸markSupported
()不适用于connection.getInputStream()
)。
public static Bitmap getBitmapFromURL(final String imageUrl, final Options options, int reqWidth, int reqHeight) {
try {
URL url = new URL(imageUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
final InputStream input = connection.getInputStream();
// using byte array to prevent open 2 times a stream
final BufferedInputStream bis = new BufferedInputStream(input, 4*1024);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte)current);
}
bis.close();
byte[] imageData = baf.toByteArray();
if(options != null){
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options optionsSize = new BitmapFactory.Options();
optionsSize.inJustDecodeBounds = true;
BitmapFactory.decodeByteArray(imageData, 0, imageData.length, optionsSize);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(optionsSize, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
optionsSize.inJustDecodeBounds = false;
}
Bitmap myBitmap = null;
if(options == null){
myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length);
}
else{
myBitmap = BitmapFactory.decodeByteArray(imageData, 0, imageData.length, options);
}
// close the stream;
input.close();
return myBitmap;
} catch (Exception e) {
return null;
}
}
private static int calculateInSampleSize(BitmapFactory.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;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
希望它会对你有所帮助。