Pregunta

how to read dynamically image from web in android, i have a method which takes String type url and return drawable, i want to get an image from remote location and set it in my imageview and through remote location change to image occur and these change automatically reflect in my android application. i have this code, any help will be appreciable, and thanks in advance

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

thanks.

¿Fue útil?

Solución

You could use an external library like Picasso or Universal-Image-Loader. URLS:

Otros consejos

Use the bellow http network connection in Asynctask or a thrad.

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

your code is a bad way to showing an remote image from url. it can caused your apps not responsive. i recommend you to using some 'image loader library' which it is take the advantages of android asynchronous task and keep your apps responsive.

for example, you can use this library: https://github.com/androidquery/androidquery/releases/tag/0.26.8

all you need to do is import the library into your project, then load your image in just a view code:

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

if u don't want to use tool or image loader library and your application not interacting to internet continuously, Then you can take XML from server at ones, that containing image url's list, parse the XML, and download them one by one on sd card and show them offline by using ImageView.setImageResource(...) this process will make your app more faster and you can set time interval to check update from server.

If u have large no of images then u have to use image loader library because above process may cause memory space issue on device .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top