Java 在Android 2.2上,从服务器下载图像位图返回null

Java 在Android 2.2上,从服务器下载图像位图返回null,java,android,Java,Android,我正在使用下面的功能从服务器下载图像。除了android 2.2 Froyo设备外,它适用于所有android版本。请帮忙 private Bitmap downloadImage(String url) { System.out.println("Splash Ad downloadImage Url " + url); Bitmap bm = null; try { URL aURL = new URL(url)

我正在使用下面的功能从服务器下载图像。除了android 2.2 Froyo设备外,它适用于所有android版本。请帮忙

    private Bitmap downloadImage(String url) {
        System.out.println("Splash Ad downloadImage Url " + url);
        Bitmap bm = null;
        try {
            URL aURL = new URL(url);
            URLConnection conn = aURL.openConnection();
            conn.connect();
            InputStream is = conn.getInputStream();
            BufferedInputStream bis = new BufferedInputStream(is);
            bm = BitmapFactory.decodeStream(bis);
            bis.close();
            is.close();
        } catch (IOException e) {
            Log.e("Hub", "Error getting the image from server : "
                    + e.getMessage().toString());
            e.printStackTrace();
        }
        return bm;
    }
试试这个代码

public class ImageDisplay extends Activity {
         ImageView image;
         ProgressBar spinner;
         TextView message;
         String path;
@Override
      public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_image_display);
    image=(ImageView) findViewById(R.id.imageDisplay);
    spinner=(ProgressBar)findViewById(R.id.progressBar_spinner);
    message=(TextView)findViewById(R.id.textView1);
    spinner.setVisibility(View.INVISIBLE);
   displayImage();
}
       private class DownloadImage extends AsyncTask<String, Void,Bitmap >
             {
Bitmap bitmap;
String error_messsage="No error";
@Override
protected Bitmap  doInBackground(String... urls) {
    for(String url:urls){
     HttpUriRequest request = new HttpGet(url.toString());
        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpResponse response = httpClient.execute(request);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
              HttpEntity entity = response.getEntity();
              byte[] bytes = EntityUtils.toByteArray(entity);
          Log.e("here",""+bytes.length); 
              bitmap = BitmapFactory.decodeByteArray(bytes, 0,bytes.length);
              Thread.sleep(1000);
            }
            else
            {
                error_messsage="Download failed, HTTP response code "+ statusCode + " - " + statusLine.getReasonPhrase();                   
            }
        } catch (Exception er) {
            Log.e("Error",""+er);
        }}
        //image.setImageBitmap(bitmap);
    return bitmap ;
}

@Override
protected void onPostExecute(Bitmap result) {
    spinner.setVisibility(View.INVISIBLE);
    image.setImageBitmap(result);

}
    }
     public void displayImage()
    {

   DownloadImage task = new DownloadImage();   
   task.execute(new String[] { "http://team-android.com/wp-             content/uploads/2011/03/Android-3d_428.jpg" });
   snap.setVisibility(View.VISIBLE);

    }
公共类ImageDisplay扩展活动{
图像视图图像;
ProgressBar旋转器;
文本视图消息;
字符串路径;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\u image\u display);
image=(ImageView)findviewbyd(R.id.imageDisplay);
微调器=(ProgressBar)findViewById(R.id.ProgressBar\u微调器);
message=(TextView)findViewById(R.id.textView1);
微调器.setVisibility(视图.不可见);
显示图像();
}
私有类DownloadImage扩展异步任务
{
位图;
字符串错误\u message=“无错误”;
@凌驾
受保护位图doInBackground(字符串…URL){
for(字符串url:url){
HttpUriRequest request=newhttpget(url.toString());
HttpClient HttpClient=新的DefaultHttpClient();
试一试{
HttpResponse response=httpClient.execute(请求);
StatusLine StatusLine=response.getStatusLine();
int statusCode=statusLine.getStatusCode();
如果(状态代码==200){
HttpEntity=response.getEntity();
字节[]字节=EntityUtils.toByteArray(实体);
Log.e(“此处”,“字节+长度”);
位图=位图工厂.decodeByteArray(字节,0,字节.长度);
睡眠(1000);
}
其他的
{
错误_message=“下载失败,HTTP响应代码”+statusCode+“-”+statusLine.getReasonPhrase();
}
}捕获(异常er){
Log.e(“错误”和“+er”);
}}
//setImageBitmap(位图);
返回位图;
}
@凌驾
受保护的void onPostExecute(位图结果){
微调器.setVisibility(视图.不可见);
image.setImageBitmap(结果);
}
}
public void displayImage()
{
DownloadImage任务=新建DownloadImage();
task.execute(新字符串[]{”http://team-android.com/wp-             content/uploads/2011/03/Android-3d_428.jpg“});
snap.setVisibility(View.VISIBLE);
}

错误是什么?您尝试了什么?没有错误/异常,它只返回空位图。您尝试了什么?您是否检查了文件是否存在,输入流是否读取,图像是否可以解码?是的,我检查了输入流是否不为空。因此,您可以尝试先下载内容,然后从本地文件打开图像。