Aller au contenu
View in the app

A better way to browse. Learn more.

Forum de Frandroid

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

[AIDE] Télécharger et afficher une image à partir d'une URL donnéen

Featured Replies

Posté(e)

Bonjour,

Je suis tout débutant dans la programmation sous android. J'ai réussi cependant à créer une petite application qui récupère sur le web une image et me l'affiche.

Cependant, l'adresse de l'image est directement donnée dans le programme, sans possibilité de modification depuis le mobile, donc. Je souhaiterai savoir comment faire pour améliorer mon programme en y ajoutant une barre de saisie dans laquelle l'utilisateur pourra entrer l'URL qu'il veut pour afficher une autre image.

Je vous joins mon programme de base.

package com.example.dl_image;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import com.example.dl_image.R;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ImageView;

public class MainActivity extends Activity {
ImageView image;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);

StrictMode.ThreadPolicy policy = new		 // Je ne sais pas à quoi ça sert
StrictMode.ThreadPolicy.Builder().permitAll().build();	 // mais c'est ce qui empèche l'application
StrictMode.setThreadPolicy(policy);		 // de crasher sans arrêt (ça désactive le StrictMode)

image = (ImageView)findViewById(R.id.ivImage);
downloadImage();
}

private void downloadImage() {
Bitmap bitmap = null;
try {
URL urlImage = new URL("http://www.coloriages-pour-enfants.com/images/dessins/chanson-le-loup-sympa-le-loup-sympa-offre-une-fleur-parent.jpg");
HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
image.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dl_image"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
 android:minSdkVersion="13"
 android:targetSdkVersion="15" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
 android:allowBackup="true"
 android:icon="@drawable/ic_launcher"
 android:label="@string/app_name"
 android:theme="@style/AppTheme" >
 <activity
	 android:name="com.example.dl_image.MainActivity"
	 android:label="@string/app_name" >
	 <intent-filter>
		 <action android:name="android.intent.action.MAIN" />
		 <category android:name="android.intent.category.LAUNCHER" />
	 </intent-filter>
 </activity>

</application>
</manifest>

Cordialement.

Posté(e)

Salut ! pour récupérer du texte entré par l'utilisateur, je te propose de créer une nouvelle classe contenant ce code:

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.onclickListener;
import android.widget.EditText;
public class PromptGetTextDialog extends AlertDialog.Builder {
public interface OnUrlPostedListener {
 public void onUrlPosted(String url);
}

private EditText mEditText;

public PromptGetTextDialog(Context context, final OnUrlPostedListener listener) {
 super(context);
 this.setTitle("Entrez l'url de l'image");
 mEditText = new EditText(context);
 this.setView(mEditText);
 this.setPositiveButton("Charger", new onclickListener() {
  public void onclick(DialogInterface arg0, int arg1) {
   listener.onUrlPosted(mEditText.getText().toString());
  }
 });
 this.setNegativeButton("Annuler", new onclickListener() {
  public void onclick(DialogInterface arg0, int arg1) {
   //annuler
  }
 });
}
}

L'idée est d'afficher une boite d'alerte à l'utilisateur, cette boite possède une champs de type EditText.

lorsque l'utilisateur clique sur le bouton "charger", le texte est récupéré et envoyé au listener.

Ce listener est une interface (OnUrlPostedListener) que ton activité 'MainActivity' peut implémenter.

Pour tester je te conseille donc de créer la classe avec le code ci-dessus et de modifier ta classe MainActivity de cette manière:

public class MainActivity extends Activity implements OnUrlPostedListener {
ImageView image;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
new PromptGetTextDialog(this, this).show();
StrictMode.ThreadPolicy policy = new			 // Je ne sais pas à quoi ça sert
StrictMode.ThreadPolicy.Builder().permitAll().build();   // mais c'est ce qui empèche l'application
StrictMode.setThreadPolicy(policy);			  // de crasher sans arrêt (ça désactive le StrictMode)
image = (ImageView)findViewById(R.id.ivImage);
downloadImage();
}
public void onUrlPosted(String url) {
 Toast.makeText(this, url, Toast.LENGTH_LONG).show();
}
private void downloadImage() {
Bitmap bitmap = null;
try {
URL urlImage = new URL("http://www.coloriages-pour-enfants.com/images/dessins/chanson-le-loup-sympa-le-loup-sympa-offre-une-fleur-parent.jpg");
HttpURLConnection connection = (HttpURLConnection) urlImage.openConnection();
InputStream inputStream = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(inputStream);
image.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}

Voilà ! si tu as des questions...

Posté(e)
  • Auteur

Merci beaucoup, mais je bloque sur un problème...

Dans la nouvelle classe créé, ceci ne veut pas fonctionner :

this.setPositiveButton("Charger", new onclickListener() {
public void onclick(DialogInterface arg0, int arg1) {
listener.onUrlPosted(mEditText.getText().toString());
}
});

this.setNegativeButton("Annuler", new onclickListener() {
public void onclick(DialogInterface arg0, int arg1) {}
});

Eclipse me souligne le onclickListener() :mellow:

J'ai bien essayé de trouver la solution avec l'ami google, mais rien n'y fait...

SI tu as une solution, je suis preneur ! :)

Et encore merci !

Edit : J'ai oublié de te poster le message d'erreur

The type new DialogInterface.onclickListener(){} must implement the inherited abstract method DialogInterface.onclickListener.onclick(DialogInterface,int)

Modifié par MadxFrog

  • 4 weeks later...
Posté(e)

il manque les majuscule "DialogInterface." et peut être un import

this.setPositiveButton("Charger", new DialogInterface.onclickListener() {

public void onclick(DialogInterface arg0, int arg1) {

listener.onUrlPosted(mEditText.getText().toString());

}

});

this.setNegativeButton("Annuler", new DialogInterfce.onclickListener() {

public void onclick(DialogInterface arg0, int arg1) {}

});

Rejoignez la conversation

Vous pouvez poster maintenant et vous enregistrez plus tard. Si vous avez un compte, connectez-vous maintenant pour poster.

Invité
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Répondre à ce sujet…

Account

Navigation

Rechercher

Rechercher

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.