Home » Android » Android App Display Image From URL

Android App Display Image From URL

Posted by: admin June 15, 2020 Leave a comment

Questions:

I’m looking for a very basic function. I’m trying to design an app and all I want it to do is load an image from a URL.
I’ve found a few questions and websites, but they all seem to be older and dated, but what I think I’m having issues with is linking the code to activity main.xml for ImageView.

Any suggestions or links you have I would greatly appreciate, thank you.

How to&Answers:

Here, this is how I display image from url in the image view
you have to call this code from thread other than main thread

ImageView img = (ImageView) findViewById(R.id.imageView1);
try {
        URL url = new URL("Your URL");
        //try this url = "http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg"
        HttpGet httpRequest = null;

        httpRequest = new HttpGet(url.toURI());

        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response = (HttpResponse) httpclient
                .execute(httpRequest);

        HttpEntity entity = response.getEntity();
        BufferedHttpEntity b_entity = new BufferedHttpEntity(entity);
        InputStream input = b_entity.getContent();

        Bitmap bitmap = BitmapFactory.decodeStream(input);

        img.setImageBitmap(bitmap);

    } catch (Exception ex) {

    }

Be careful don’t forget to surround the code with try catch(I have already done that in this code)

or you can use webview to load image from url

WebView web = (WebView) findViewById(R.id.webView1);
web.loadUrl("Your Url");

if you are trying to load image from the assets folder url will start like this
“file:///android_asset/yourimage.jpg”
else normal internet url like this
http://0.tqn.com/d/webclipart/1/0/5/l/4/floral-icon-5.jpg

hope this works for you
Good Luck

Answer:

There is an opensource library called imageloader. It is widely used, you can use it directly or make code similar to it.

https://github.com/nostra13/Android-Universal-Image-Loader

Answer:

You can take the image and on your php side convert it into a base64 and then on Android side decode it into a image.

Answer:

The best practice to download image is to be done on the background Thread, so that it doesn’t interrupt your Main Thread,then update the User Interface as needed.

public class MainActivity extends AppCompatActivity {

FrameLayout frameLayout;
ImageView imageView;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    frameLayout= (FrameLayout) findViewById(R.id.containerlayout);
    imageView= (ImageView) findViewById(R.id.imageView);
    progressBar= (ProgressBar) findViewById(R.id.progressBar);

    String url="http://www.flat-e.com/flate5/wp-content/uploads/cover-960x857.jpg";
    MyTask myTask= new MyTask();
    myTask.execute(url);
}
class MyTask extends AsyncTask<String, Void, Bitmap> {

    @Override
    protected void onPreExecute() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    protected Bitmap doInBackground(String... voids) {

        Bitmap bitmap=null;
        try {
            URL url =new URL(voids[0]);
            HttpURLConnection connection= (HttpURLConnection) url.openConnection();
            InputStream inputStream= connection.getInputStream();
            bitmap = BitmapFactory.decodeStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return bitmap;
    }

    @Override
    protected void onPostExecute(Bitmap bitmap) {
        progressBar.setVisibility(View.GONE);
        imageView.setImageBitmap(bitmap);

    }
}

}

Here, in this example I created an inner class MyTask which extends the AsyncTask where i have done all my network operations. Make sure to add the Uses permission in your Manifest File.

Hope this works for you too.