grace
grace

Reputation: 73

My app is displaying the image url. How do i download the image so that it can display on my app

//my adapter. I'm trying to find a way to implement Picasso in the code below or the activity for it to display the image. I'm also using a gridview for displaying the images. The getImage() below is the method responsible for getting the url

        public CategoryViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
            mContext = itemView.getContext();
        }

        public void bindCategory(Category category) {
            mNameTextView.setText(category.getImage());
        }
    }
}

Upvotes: 0

Views: 106

Answers (3)

AbuQauod
AbuQauod

Reputation: 919

You can view the image like that , using a custom lib

Picasso.with(this).load(getImage()).into(imageView);

or

Glide.with(this).load(getImage()).into(imageView);

and downloading the image:

public void DownloadImageFromPath(String path){
            InputStream in =null;
            Bitmap bmp=null;
             ImageView iv = (ImageView)findViewById(R.id.img1);
             int responseCode = -1;
            try{

                 URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
                 HttpURLConnection con = (HttpURLConnection)url.openConnection();
                 con.setDoInput(true);
                 con.connect();
                 responseCode = con.getResponseCode();
                 if(responseCode == HttpURLConnection.HTTP_OK)
                 {
                     //download 
                     in = con.getInputStream();
                     bmp = BitmapFactory.decodeStream(in);
                     in.close();
                     iv.setImageBitmap(bmp);
                 }

            }
            catch(Exception ex){
                Log.e("Exception",ex.toString());
            }
        }

Upvotes: 0

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

You can get the InputStream from the url, and decode a bitmap to display it in a ImageView.

try {
    URL url = new URL(getImage());
    Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
    imageView.setImageBitmap(bitmap);
} catch (IOException e) {
    e.printStackTrace();
}

I recommend you to use Picasso library, using which you can easily load images using following code:

Picasso.with(this/*context*/).load(getImage()).into(imageView);

Edit:

As you wished to get image directly from the method, here it goes:

public Bitmap getImage(){
    String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()
        +"/"+getId()+"_"+getSecret()+"_m.jpg";
    try {
        URL url = new URL(imageUrl);
        return BitmapFactory.decodeStream(url.openStream());
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

Upvotes: 1

Lokkesh
Lokkesh

Reputation: 570

You can open the Image in a Webview. Make sure you have a WebView component in your Activity Xml also Declare the WebView variable outside of your Class ( Global variable ) So that u can use it inside a function.

public void getImage(){
        String imageUrl  = "https://farm"+getFarm()+".staticflickr.com/"+getServer()+"/"+getId()+"_"+getSecret()+"_m.jpg";
        mWebView.loadDataWithBaseURL(null, "<html><head></head><body><table style=\"width:100%; height:100%;\"><tr><td style=\"vertical-align:middle;\"><img src=\"" + imageUrl + "\"></td></tr></table></body></html>", "html/css", "utf-8", null);

    }

Upvotes: 0

Related Questions