Good morning, I have encountered a problem when making my android app with android studio.
What I need is to take a .png image of a server (given a url) using the GET method and display it on the screen.
I have seen some old tutorials but the syntax is now different and does not work correctly. Can someone give me an example of how the code would be to use or pass me an updated link.
This is the code I'm trying to use but it does not work for me: (
I am novatillo in what android applications are concerned and I would need a cable. Thanks in advance and a greeting
XML:
<?xml version="1.0" encoding="utf-8" ?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView android:id="@+id/image_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
.java
package com.jonsegador.examples.externalimage;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.Toast;
public class Main extends Activity {
private ImageView imageView;
private Bitmap loadedImage;
private String imageHttpAddress = "http://jonsegador.com/wp-content/apezz.png";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.image_view);
downloadFile(imageHttpAddress);
}
void downloadFile(String imageHttpAddress) {
URL imageUrl = null;
try {
imageUrl = new URL(imageHttpAddress);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.connect();
loadedImage = BitmapFactory.decodeStream(conn.getInputStream());
imageView.setImageBitmap(loadedImage);
} catch (IOException e) {
Toast.makeText(getApplicationContext(), "Error cargando la imagen: "+e.getMessage(), Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
}
source: link