• Latest Code...

    Load Image From Web url in Android or Android Studio

    Here, is the tutorial for image url in to the your mobile app.
    There are two method, you can choose as your requirement ,  Aquey or direct image load,

    1. Image load using direct url

    First you have defined , imageview and then load image url in that imageview.

    private Bitmap bitmapImage;
    private ImageView imageView;
    private ProgressDialog pdDialog;

    private String imageUrl=""https://www.....png";

    In onCreate:

    imageView= (ImageView)findViewById(R.id.imgView);
    new LoadImageFromUrl ().execute(imageUrl);

    now defined LoadImageFromUrl, outsite the onCreate:


     private class LoadImageFromUrl extends AsyncTask<String, String, Bitmap> {
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                pdDialog = new ProgressDialog(MainActivity.this);
                pdDialog.setMessage("Loading Image From URL ....");
                pdDialog.show();

            }
             protected Bitmap doInBackground(String... args) {
                 try {
                       bitmap = BitmapFactory.decodeStream((InputStream)new URL(args[0]).getContent());

                } catch (Exception e) {
                      e.printStackTrace();
                }
                return bitmap;
             }

             protected void onPostExecute(Bitmap image) {

                 if(image != null){
                 imageView.setImageBitmap(image);
                 pDialog.dismiss();

                 }else{

                 pDialog.dismiss();
                 Toast.makeText(MainActivity.this, "Image Does Not exist in such url or Network Error", Toast.LENGTH_SHORT).show();

                 }
             }
         }


    2. Image Load using Aquery


    import com.androidquery.AQuery;

    public class AQueryImageLoading extends Activity {
         private AQuery aq;
        ProgressDialog pDialog;
    private String imageUrl=""https://www.....png";
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
         
            aq = new AQuery(this);
                   aq.id(R.id.loadProgressDialogBtn).text("Load Image with progress dialog bar").clicked(this, "loadImageFromInternetWithProgressDialogBar");
         
        }
     
        // Call back method for Button - R.id.loadProgressDialogBtn click
        public void loadImageFromInternetWithProgressDialogBar(View image) {
            pDialog= new ProgressDialog(this);
            pDialog.setMessage("Downloading Image from Internet. Please wait...");
            pDialog.setIndeterminate(false);
            pDialog.setMax(100);
            pDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
            pDialog.setCancelable(false);
            Toast.makeText(aq.getContext(), "Download initiated...",Toast.LENGTH_SHORT).show();
            // Display Progress Dialog Bar by invoking progress method
            aq.id(R.id.loadProgressDialogImg).progress(prgDialog).image(imageUrl,false,false);
        }

    }



    Happy Coding !!!

    Circle

    Most recent Elastic Search tutorials for beginner, intermediate and advance level developer, Yubraj Pokharel, in CodingWorkspace