public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         Bitmap bitmap = DownloadImage(
                    "http://www.animateit.net/data/media/nov2011/1590877-smiling_kid.gif");
                ImageView img = (ImageView) findViewById(R.id.img);
                img.setImageBitmap(bitmap);
            }
            private InputStream OpenHttpConnection(String urlString) 
            throws IOException
            {
                InputStream in = null;
                int response = -1;
                URL url = new URL(urlString); 
                URLConnection conn = url.openConnection();
                if (!(conn instanceof HttpURLConnection))                     
                    throw new IOException("Not an HTTP connection");
                try{
                    HttpURLConnection httpConn = (HttpURLConnection) conn;
                    httpConn.setAllowUserInteraction(false);
                    httpConn.setInstanceFollowRedirects(true);
                    httpConn.setRequestMethod("GET");
                    httpConn.connect(); 
                    response = httpConn.getResponseCode();                 
                    if (response == HttpURLConnection.HTTP_OK) {
                        in = httpConn.getInputStream();                                 
                    }                     
                }
                catch (Exception ex)
                {
                    throw new IOException("Error connecting");            
                }
                return in;     
            }
            private Bitmap DownloadImage(String URL)
            {        
                Bitmap bitmap = null;
                InputStream in = null;        
                try {
                    in = OpenHttpConnection(URL);
                    bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                } catch (IOException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                }
                return bitmap;                
            }
    }我需要从服务器数据库中检索图像。我已经尝试过此代码,但未显示result.Then。此源程序已成功运行,但未显示图像。我找不到错误,任何人都可以帮助,为什么图像未显示和下载。
发布于 2014-04-19 17:53:25
这里有一个简单的方法,可以让你做到这一点:
public static Bitmap decodeRemoteBitmap(String urlString) throws MalformedURLException, IOException{
    URL url = new URL(urlString);
    InputStream inputStream = url.openStream();
    Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
    return bitmap;
}发布于 2014-04-19 18:09:16
在代码中问题:在线程中加载位图,而不是UI线程
1-在异步任务中添加位图加载,如下所示:
public class ImageLoader extends AsyncTask<String, Integer, Bitmap>{
private ImageView img;
public ImageLoader(ImageView img) {
    this.img = img;
}
@Override
protected Bitmap doInBackground(String... params) {
    try {
        InputStream in = OpenHttpConnection(params[0]);
        Bitmap bitmap = BitmapFactory.decodeStream(in);
        in.close();
        return bitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}
@Override
protected void onPostExecute(Bitmap result) {
    if (result != null)
        img.setImageBitmap(result);
    super.onPostExecute(result);
}
  }2-调用任务,方法如下:
ImageLoader loader = new ImageLoader(img);
loader.execute(URL);Other solution在加载图像时有很多情况需要处理,比如outOfMemory和缓存,使用库可以节省时间并减少错误。
我使用Android-Universal-Image-Loader很简单,在lib文件夹中添加jar文件,然后添加ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(imageUri, imageView);
发布于 2014-04-19 18:34:21
您可以使用Alex提到的Android Universal Image Loader,这是我的首选(Picasso是另一个选项)。然而,最近我在这些库中遇到了非常具体的问题,这导致我使用异步任务自己实现了一个简单的图像加载器:
public class ImageDownloadTask extends AsyncTask<String, Void, Bitmap> {
    private final String imageURL;
    private final ImageView imageView;
    private final Context context;
    /**
     * Constructor
     * @param context
     * @param imageURL
     * @param imageView
     */
    public ImageDownloadTask(final Context context, final String imageURL, final ImageView imageView) {
        this.imageURL = imageURL;
        this.imageView = imageView;
        this.context = context;
    }
    @Override
    protected Bitmap doInBackground(final String... args) {
        Bitmap bitmap = null;
        try {
                final InputStream in = new URL(imageURL).openStream();
                bitmap = BitmapFactory.decodeStream(in);
                in.close();
        } catch (final Exception e) {
            Log.e("Error", e.getMessage());
        }
        return bitmap;
    }
    @Override
    protected void onPostExecute(final Bitmap result) {
        if(result != null){
            imageView.setImageBitmap(result);
        }
    }
}在活动中,您可以像这样使用这个类:
new ImageDownloadTask(this, pictureURL, surveyImage).execute();https://stackoverflow.com/questions/23168472
复制相似问题