此函数在某些图像中返回Null,图像扩展名为webp,在模拟器中我没有问题,但是一个客户端有一个Lenovo S6000,有些页面返回NULL值......
问题是:是否存在另一种方法来读取给定URL中的图像?有人请帮助我。
Bitmap downloadBitmap(String url, Integer type){
int scale=1;
Integer contW = containerWidth, contH = containerHeight;
int width_tmp=1200, height_tmp=1600;
final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
final HttpClient client2 = new DefaultHttpClient();
final HttpGet getRequest = new HttpGet(url);
HttpResponse response = null;
try {
response = client.execute(getRequest);
} catch (Exception e) {
System.out.println(e);
response = null;
try{
response = client2.execute(getRequest);
}catch(Exception e2){
System.out.println(e2);
return null;
}
}
try{
final int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK){
System.out.println("Status error ");
return null;
}
HttpEntity entity = response.getEntity();
if (entity == null){
System.out.println("httpentity error ");
return null;
}
if(contH==0 || contW==0){
scale = 5;
contW = width_tmp;
contH = height_tmp;
}
final int REQUIRED_SIZE=Math.min(contH, contW);
while(true){
if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
break;
width_tmp/=2;
height_tmp/=2;
scale++;
}
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize=scale;
options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.RGB_565;
options.inTargetDensity = context.getResources().getDisplayMetrics().densityDpi;
options.inPurgeable=true;
options.inTempStorage = new byte[16 * 1024];
InputStream inputStream = null;
BufferedInputStream buffer = null;
BufferedHttpEntity bufferedEntity = null;
Bitmap img = null;
try {
if(type==0){
bufferedEntity = new BufferedHttpEntity(entity);
inputStream = bufferedEntity.getContent();
}else{
inputStream = (InputStream) new URL(url).getContent();
}
buffer=new BufferedInputStream(inputStream,16*1024);
img = BitmapFactory.decodeStream(buffer,null,options);
if (img != null && inputStream != null) inputStream.close();
if(buffer != null) buffer.close();
if(bufferedEntity != null) bufferedEntity.consumeContent();
if(img != null && entity != null) entity.consumeContent();
} catch (IllegalStateException e) {
System.out.println(e);
img = null;
} catch (IOException e) {
img = null;
System.out.println(e);
}catch(Exception e){
img = null;
System.out.println(e);
}
error += "--->METODO "+(type==0?"1":"3")+"--->";
if(img != null)
return img;
FlushedInputStream buffer2 = null;
try {
if(type==0)
inputStream.reset();
buffer2 = new FlushedInputStream(inputStream);
img = BitmapFactory.decodeStream(buffer2,null,options);
if(inputStream != null) inputStream.close();
if(buffer2 != null) buffer2.close();
if(entity != null) entity.consumeContent();
} catch (IllegalStateException e) {
System.out.println(e);
img = null;
} catch (IOException e) {
img = null;
System.out.println(e);
}catch(Exception e){
img = null;
System.out.println(e);
}
error += "--->METODO "+(type==0?"2":"4")+"--->";
if(img != null)
return img;
}finally{
if (client != null) client.close();
getRequest.abort();
}
if(type==0)
return downloadBitmap(url,1);
error += "--->THE SAME--->";
return null;
}