J'essaie de décoder à bipmap de flux d'entrée URLAndroid async obtenir une image
public class Présentateur implémente MVPmain.presenter {
String LOG_TAG = "Presenter: ";
private final MVPmain.view view;
String url = "https://jsonplaceholder.typicode.com/";
public Presenter(MVPmain.view view) {
this.view = view;
}
void photosUrl() {
String photoUrl = "http://placehold.it/600/92c952";
AsyncLoadImage asyncLoadImage = new AsyncLoadImage();
asyncLoadImage.execute(photoUrl);
}
}
@Override
public void button_photos_clicked() {
photosUrl();
}
Bitmap loadImage(String url) {
Bitmap bitmapImage = null;
URL imageUrl = null;
HttpURLConnection httpURLConnection = null;
try {
imageUrl = new URL(url.replaceAll("\\r|\\n", ""));
httpURLConnection = (HttpURLConnection) imageUrl.openConnection();
bitmapImage = BitmapFactory.decodeStream(httpURLConnection.getInputStream());
Log.d(LOG_TAG, "Runneble: " + "OK");
} catch (MalformedURLException e) {
Log.e(LOG_TAG, "image url error: " + e);
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return bitmapImage;
}
class AsyncLoadImage extends AsyncTask<String, Void, Bitmap> {
String LOG_TAG = "AsyncLoadImage: ";
Bitmap bitmapImage;
@Override
protected Bitmap doInBackground(String... strings) {
String request = "";
final URL imageUrl = null;
JSONObject jsonObject = null;
for (final String address : strings) {
Log.d(LOG_TAG, "img url: " + address);
bitmapImage = loadImage(address);
}
return bitmapImage;
}
@Override
protected void onPostExecute(Bitmap bitmapImg) {
view.setPhotos(bitmapImg);
}
}
}
c'est une partie de code, où je reçois exception: Connexion refusée dans httpURLConnection.getInputStream(). J'essaie .getContent et reçois cette erreur aussi. S'il vous plaît, dites-moi, comment je peux le faire.
SOLVED Ce cas d'erreur Adway!)
avez-vous ajouté dans le manifeste –
Oui. Au début, je reçois l'adresse de la photo d'ici: https://jsonplaceholder.typicode.com/photos/3, asynctask travailler avec cette URL, mais ne fonctionne pas avec l'URL de l'image: http://placehold.it/600/92c952 . Par exemple cette url: https://image.flaticon.com/teams/slug/freepik.jpg travail ... –