Comment afficher une image depuis une URL sur Android

Je veux afficher une image à l’écran. L’image doit provenir de l’URL et ne peut pas être dessinée.

Le code est ici:

 

mais cela donne une erreur au moment de la compilation.

Comment afficher une image depuis une URL dans Android?

Vous pouvez afficher directement l’image à partir du Web sans la télécharger. Veuillez vérifier la fonction ci-dessous. Il affichera les images du Web dans votre vue d’image.

 public static Drawable LoadImageFromWebOperations(Ssortingng url) { try { InputStream is = (InputStream) new URL(url).getContent(); Drawable d = Drawable.createFromStream(is, "src name"); return d; } catch (Exception e) { return null; } } 

définissez ensuite l’image sur imageview en utilisant le code de votre activité.

Vous pouvez essayer ceci que je trouve dans une autre question.

Android, Créer une image à une URL égale à l’image d’ImageView

 try { ImageView i = (ImageView)findViewById(R.id.image); Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL(imageUrl).getContent()); i.setImageBitmap(bitmap); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } 

J’ai essayé ce code en travaillant pour moi, obtenez l’image directement de l’url

  private class DownloadImageTask extends AsyncTask { ImageView bmImage; public DownloadImageTask(ImageView bmImage) { this.bmImage = bmImage; } protected Bitmap doInBackground(Ssortingng... urls) { Ssortingng urldisplay = urls[0]; Bitmap mIcon11 = null; try { InputStream in = new java.net.URL(urldisplay).openStream(); mIcon11 = BitmapFactory.decodeStream(in); } catch (Exception e) { Log.e("Error", e.getMessage()); e.printStackTrace(); } return mIcon11; } protected void onPostExecute(Bitmap result) { bmImage.setImageBitmap(result); } } 

utiliser la méthode onCreate ()

new DownloadImageTask ((ImageView) findViewById (R.id.image)) .execute (” http://scoopak.com/wp-content/uploads/2013/06/free-hd-natural-wallpapers-download-for-pc .jpg “);

Vous pouvez essayer avec Picasso, c’est vraiment sympa et facile. N’oubliez pas d’append les permissions dans le manifeste.

 Picasso.with(context) .load("http://ImageURL") .resize(width,height) .into(imageView ); 

Vous pouvez également consulter un tutoriel ici: Youtube / Github

Pour un exemple simple,
http://www.helloandroid.com/tutorials/how-download-fileimage-url-your-device

Vous devrez utiliser httpClient et télécharger l’image (la mettre en cache si nécessaire),

solution offerte pour l’affichage d’images dans listview, essentiellement le même code (vérifiez le code où imageview est défini à partir d’url) pour l’affichage.

Charge paresseuse d’images dans ListView

J’ai réessayé une image à partir d’une URL et stockée sur ma carte SD en utilisant le code suivant:

 public Ssortingng Downloadfromurl(Ssortingng Url) { Ssortingng filepath=null; try { URL url = new URL(Url); //create the new connection HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); //set up some things on the connection urlConnection.setRequestMethod("GET"); urlConnection.setDoOutput(true); //and connect! urlConnection.connect(); //set the path where we want to save the file //in this case, going to save it on the root directory of the //sd card. folder = new File(Environment.getExternalStorageDirectory().toSsortingng()+"/img"); folder.mkdirs(); //create a new file, specifying the path, and the filename //which we want to save the file as. Ssortingng filename= "page"+no+".PNG"; file = new File(folder,filename); if(file.createNewFile()) { file.createNewFile(); } //this will be used to write the downloaded data into the file we created FileOutputStream fileOutput = new FileOutputStream(file); //this will be used in reading the data from the internet InputStream inputStream = urlConnection.getInputStream(); //this is the total size of the file int totalSize = urlConnection.getContentLength(); //variable to store total downloaded bytes int downloadedSize = 0; //create a buffer... byte[] buffer = new byte[1024]; int bufferLength = 0; //used to store a temporary size of the buffer //now, read through the input buffer and write the contents to the file while ( (bufferLength = inputStream.read(buffer)) > 0 ) { //add the data in the buffer to the file in the file output stream (the file on the sd card fileOutput.write(buffer, 0, bufferLength); //add up the size so we know how much is downloaded downloadedSize += bufferLength; //this is where you would do something to report the prgress, like this maybe Log.i("Progress:","downloadedSize:"+downloadedSize+"totalSize:"+ totalSize) ; } //close the output stream when done fileOutput.close(); if(downloadedSize==totalSize) filepath=file.getPath(); //catch some possible errors... } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { filepath=null; e.printStackTrace(); } Log.i("filepath:"," "+filepath) ; return filepath; } 
 InputStream URLcontent = (InputStream) new URL(url).getContent(); Drawable image = Drawable.createFromStream(URLcontent, "your source link"); 

cela a fonctionné pour moi

Ecrivez le code en utilisant ASyncTask pour la gestion http.

 Bitmap b; ImageView img; ...... try { URL url = new URL("http://10.119.120.10:80/img.jpg"); InputStream is = new BufferedInputStream(url.openStream()); b = BitmapFactory.decodeStream(is); } catch(Exception e){} ...... img.setImageBitmap(b); 

J’ai le même problème. Je teste ce code et fonctionne bien. Ce code Obtenir l’image à partir de l’URL et mettre – “bmpImage”

 URL url = new URL("http://your URL"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setReadTimeout(60000 /* milliseconds */); conn.setConnectTimeout(65000 /* milliseconds */); conn.setRequestMethod("GET"); conn.setDoInput(true); conn.connect(); int response = conn.getResponseCode(); //Log.d(TAG, "The response is: " + response); is = conn.getInputStream(); BufferedInputStream bufferedInputStream = new BufferedInputStream(is); Bitmap bmpImage = BitmapFactory.decodeStream(bufferedInputStream);