图像很小(400Kb),但需要很长时间才能读取。以下代码中的第一个while循环需要很多次(有些情况下它超级快)。 Any reason for this behavior, how can I make this consistent
。此代码在asyctask中执行,如下所示。其中还显示了进度对话框。
更多详细信息:我正在从服务器下载png。我的目标是获取图像边界并对其进行采样以避免内存问题。如果我只使用一个输入流来获取边界,我就无法再次使用它来获取图像。这就是为什么我要创建两个副本。问题在于while循环以不可预测的方式长期存在。有时加载速度非常快。
public static Bitmap decodeBitmapFromInputStream(InputStream inputStream,
int reqWidth, int reqHeight, boolean checkLen) {
byte[] byteArr = new byte[0];
byte[] buffer = new byte[1024];
int len;
int count = 0;
try {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
while ((len = inputStream.read(buffer)) > -1 ) {
baos.write(buffer, 0, len);
}
baos.flush();
InputStream is1 = new ByteArrayInputStream(baos.toByteArray());
InputStream is2 = new ByteArrayInputStream(baos.toByteArray());
/*while ((len = inputStream.read(buffer)) > -1) {
if (len != 0) {
if (count + len > byteArr.length) {
byte[] newbuf = new byte[(count + len) * 2];
System.arraycopy(byteArr, 0, newbuf, 0, count);
byteArr = newbuf;
}
System.arraycopy(buffer, 0, byteArr, count, len);
count += len;
}
}*/
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
// BitmapFactory.decodeByteArray(byteArr, 0, count, options);
BitmapFactory.decodeStream(is1, null, options);
if (checkLen) {
if (options.outHeight == options.outWidth) {
reqWidth = (int) (reqWidth * 0.9);
reqHeight = reqWidth;
} else {
reqWidth = (int) (reqWidth * 0.75);
reqHeight = (int) (reqWidth * 0.75);
}
}
options.inSampleSize = calculateInSampleSize(options, reqWidth,
reqHeight);
options.inPurgeable = true;
options.inInputShareable = true;
options.inJustDecodeBounds = false;
//Bitmap b = BitmapFactory.decodeByteArray(byteArr, 0, count, options);
Bitmap b = BitmapFactory.decodeStream(is2, null, options);
Matrix matrix = new Matrix();
int width = b.getWidth();
int height = b.getHeight();
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) reqWidth) / width;
float scaleHeight = ((float) reqHeight) / height;
matrix.postScale(scaleWidth, scaleHeight);
Bitmap scaled = b.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(),
matrix, true);
b.recycle();
b = null;
return scaled;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
protected Bitmap doInBackground(String... param) {
Log.w("myApp", "doInBackground");
Bitmap bm = null;
InputStream is = null;
if (mode == 1 || mode == 3) {
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.setConnectTimeout(Utils.timeout);
conn.setReadTimeout(Utils.timeout);
Log.w("myApp", "doInBackground- connect");
conn.connect();
Log.w("myApp", "doInBackground- Image Request");
is = conn.getInputStream();
Log.w("myApp", "doInBackground- Image recived");
bm = Utils.decodeBitmapFromInputStream(is, width, width, true);
Log.w("myApp", "doInBackground-Decoded");
} catch (IOException e) {
if (ringProgressDialog != null && ringProgressDialog.isShowing())
ringProgressDialog.dismiss();
} finally {
try {
if (is != null)
is.close();
} catch (Exception e) {
}
}
}
return bm;
}