在android中从web/api(.net)动态读取图像

在android中从web/api(.net)动态读取图像,android,remote-server,Android,Remote Server,如何在android中从web动态读取图像,我有一个方法,该方法采用字符串类型url并返回drawable,我想从远程位置获取图像,并在imageview中进行设置,然后通过远程位置更改来生成图像,这些更改会自动反映在我的android应用程序中。 我有这个代码,任何帮助都将是可观的,并提前感谢 public static Drawable LoadImageFromWeb(String url){ try{ InputStream is = (InputStream)

如何在android中从web动态读取图像,我有一个方法,该方法采用字符串类型url并返回drawable,我想从远程位置获取图像,并在imageview中进行设置,然后通过远程位置更改来生成图像,这些更改会自动反映在我的android应用程序中。 我有这个代码,任何帮助都将是可观的,并提前感谢

public static Drawable LoadImageFromWeb(String url){
    try{
        InputStream is = (InputStream) new URL(url).getContent();
        Drawable drw = Drawable.createFromPath(url2);
        //Drawable draw = Drawable.createFromStream(is, url2);
        return drw;


    }catch(Exception ex){
        ex.printStackTrace();
        return null;
    } 

谢谢。

在Asynctask或thrad中使用下面的http网络连接

            HttpParams httpParameters = new BasicHttpParams();
                // Set the timeout in milliseconds until a connection is established.
                // The default value is zero, that means the timeout is not used.           
                int timeoutConnection = 20000;
                HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
                // Set the default socket timeout (SO_TIMEOUT) 
                // in milliseconds which is the timeout for waiting for data.
                int timeoutSocket = 30000;
                HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);               
                httpclient = new DefaultHttpClient(httpParameters);
                HttpPost httppost = new HttpPost(url);
                //Adding the parameter values (if required input to remote service)
                List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
                nameValuePairs.add(new BasicNameValuePair(Constants.URL_PARAM_PROVIDERID,Utilities.getProviderID(PageCurlGraph.this)));
                nameValuePairs.add(new BasicNameValuePair("requestedDate",params[i]));
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                //Execute HTTP Post Request
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity httpEntity = response.getEntity();
                InputStream instream = httpEntity.getContent();
                Bitmap bitmap  = BitmapFactory.decodeStream(instream);
                gm = new GraphModel();
                gm.setBitmap(bitmap);
HttpParams httpParameters=new BasicHttpParams();
//以毫秒为单位设置超时,直到建立连接。
//默认值为零,表示不使用超时。
int timeoutConnection=20000;
HttpConnectionParams.setConnectionTimeout(httpParameters,timeoutConnection);
//设置默认套接字超时(SO\U超时)
//以毫秒为单位,这是等待数据的超时。
int timeoutSocket=30000;
HttpConnectionParams.setSoTimeout(httpParameters,timeoutSocket);
httpclient=新的默认httpclient(httpParameters);
HttpPost HttpPost=新的HttpPost(url);
//添加参数值(如果需要,输入远程服务)
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(Constants.URL\u PARAM\u PROVIDERID,Utilities.getProviderID(PageCurlGraph.this));
添加(新的BasicNameValuePair(“requestedDate”,参数[i]);
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
HttpEntity HttpEntity=response.getEntity();
InputStream instream=httpEntity.getContent();
位图位图=位图工厂.decodeStream(流内);
gm=新图形模型();
gm.setBitmap(位图);

您可以使用外部库,如毕加索或通用图像加载器。网址:


您的代码是从url显示远程图像的糟糕方式。这可能会导致你的应用程序没有响应。 我建议你使用一些“图像加载器库”,它是利用android异步任务的优势,并保持你的应用程序响应

例如,您可以使用此库:

您只需将库导入到项目中,然后在视图代码中加载图像:

ImageView imgView = (ImageView) findViewById(R.id.imageview);
AQuery aq = new AQuery(this); // assume you are doing this inside activity
String someImageURL = "http://someurl.com/someimage.jpg";

aq.id(imgView).image(someImageURL, true, true); // see documentation for detail

如果您不想使用工具或图像加载器库,并且您的应用程序不连续地与internet交互,那么 您可以从包含图像url列表的服务器上获取XML,解析XML,然后在sd卡上逐个下载并使用ImageView脱机显示。setImageResource(…)此过程将使您的应用程序更快,并且您可以设置检查服务器更新的时间间隔


若你们并没有大量的图像,那个么你们必须使用图像加载器库,因为上述过程可能会导致设备上的内存空间问题

首先,感谢您的回复,如果您告诉我们这些键值nameValuePairs.add(新的BasicNameValuePairs(Constants.URL\u PARAM\u PROVIDERID,Utilities.getProviderID(PageCurlGraph.this));添加(新的BasicNameValuePair(“requestedDate”,参数[i]);这些用于输入到远程服务器。如果不需要任何输入,请删除NameValuePair列表。您是否可以使用毕加索共享一些示例代码?您为毕加索提供的链接有点不可理解,因为我从未在github中使用过此库。您有一个示例应用程序,可以帮助您了解毕加索的工作原理=)