【发布时间】:2011-03-19 06:43:56
【问题描述】:
我尝试了三种下载图像的方法。所有这些都由 Stackoverflow 的成员提出。 这三种方法都无法从服务器下载所有图像。很少下载,也很少。
我注意到每个方法都无法从特定位置下载图像。 那就是方法3总是无法下载前三个图像。我更改了图像,但即便如此,前三张图像也没有下载。
方法一:
public Bitmap downloadFromUrl( String imageurl )
{
Bitmap bm=null;
String imageUrl = imageurl;
try {
URL url = new URL(imageUrl); //you can write here any link
URLConnection ucon = url.openConnection();
InputStream is = ucon.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1) {
baf.append((byte) current);
}
bm= BitmapFactory.decodeByteArray(baf.toByteArray(), 0, baf.toByteArray().length);
} catch (IOException e) {
Log.d("ImageManager", "Error: " + e);
}
return bm;
}
这里我得到的丢失图像的错误是 :SKIimagedecoder ,工厂返回 null。
方法:2
public static Bitmap loadBitmap(String url)
{
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 4*1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 4 * 1024);
int byte_;
while ((byte_ = in.read()) != -1)
out.write(byte_);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
System.out.println(e);
Log.e("","Could not load Bitmap from: " + url);
} finally {
try{
in.close();
out.close();
}catch( IOException e )
{
System.out.println(e);
}
}
return bitmap;
}
我在这里得到的错误和上面一样。
方法三:
private Bitmap downloadFile(String fileUrl){
URL bitmapUrl =null;
Bitmap bmImg = null;
try {
bitmapUrl= new URL(fileUrl);
} catch (MalformedURLException e) {
e.printStackTrace();
}
HttpGet httpRequest = null;
try {
httpRequest = new HttpGet(bitmapUrl.toURI());
} catch (URISyntaxException e) {
e.printStackTrace();
}
try {
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = (HttpResponse) httpclient
.execute(httpRequest);
HttpEntity entity = response.getEntity();
BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity);
InputStream instream = bufHttpEntity.getContent();
bmImg = BitmapFactory.decodeStream(instream);
} catch (Exception e) {
System.out.println(e);
}
return bmImg;
}
我在这里得到的错误是:org.apache.http.NoHttpResponseException:目标服务器未能响应。
请帮忙。这是阻止我完成项目的唯一原因。
【问题讨论】:
-
它是仅在一台服务器上执行此操作,还是在所有服务器上执行此操作?会不会是服务器端的问题?您是否尝试过从其他位置下载文件,只是为了测试?
-
不,我没有尝试从其他服务器下载。
-
请尝试一下。看到你正在尝试 3 种不同的方法(没有检查过,但我假设你有),我的第一个怀疑是事情的另一面。 (或介于两者之间的东西,比如连接)
-
是的,他们都在工作。我没有其他服务器。
-
@rajivpradeep 那么您应该对答案发表评论,以描述尝试时会发生什么。如果您不与我们合作,没有人可以帮助您
标签: android