如何从android中的服务器检索图像

时间:2014-04-19 09:46:49

标签: android

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;                
            }


    }

我需要从服务器db中检索图像。我已经尝试过这段代码,但没有显示结果。然后这个源被成功运行但没有显示images.i找不到任何人可以帮助的错误,为什么图像没有显示并下载。

4 个答案:

答案 0 :(得分:1)

这是一个允许您这样做的简单方法:

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;
}

答案 1 :(得分:0)

代码中的问题:在线程中加载位图而不是ui线程

1-在Async任务中添加位图加载,如下所示:

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);

其他解决方案 在加载像outOfMemory和缓存这样的图像时,你需要处理很多情况,使用库它会节省你的时间和错误。

我使用Android-Universal-Image-Loader这很简单,然后在lib文件夹中添加jar文件 ImageLoader.getInstance().init(config);
ImageLoader.getInstance().displayImage(imageUri, imageView);

答案 2 :(得分:0)

您可以使用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();

答案 3 :(得分:0)

这是一段代码:

                URL url = new URL(image);
                HttpURLConnection conn = (HttpURLConnection) url
                .openConnection();
                 conn.setDoInput(true);
                 conn.connect();
                 InputStream is = conn.getInputStream();
           bmImg = BitmapFactory.decodeStream(is);
            imgdisplay.setImageBitmap(bmImg);           

&#34;图像&#34;是imagepath。还要定义像Bitmap bmImg = null;

这样的位图