Pergunta

I am trying to load image from url to text view , but unable to do it, their is no error, progress bar is also appearing ,but image is not loading to image view , please help, thanks in advance

my code..

1. XML :

<ImageView
    android:id="@+id/imageView"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="5dp"
     />

2. java code....

private class Load_Product_Image extends
AsyncTask<Void, Drawable, Drawable> {

     private Object fetch_image(String webaddress)
    throws MalformedURLException, IOException {
URL url = new URL(webaddress);
Object content = url.getContent();
return content;
}

protected void onPreExecute() {
    super.onPreExecute();
    progressDialog = new ProgressDialog(getActivity(),ProgressDialog.THEME_TRADITIONAL);
    progressDialog.setMessage("Loading...");
    progressDialog.setIndeterminate(false);
    progressDialog.setCancelable(false);
    progressDialog.show();
}

@Override
protected Drawable doInBackground(Void... params) {

    InputStream is = null;
    try {
        is = (InputStream) this.fetch_image(image_url);// image uri
                                                        // address
                                                        // getting
                                                        // from
                                                        // previous
                                                        // toplist
                                                        // activity
                                                        // using
                                                        // intent

    } catch (MalformedURLException e) {

        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
    Drawable draw = Drawable.createFromStream(is, "src");
    return draw;
}

    protected void onPostExecute(final Drawable result) {
        try {
            // Display Image...
            runOnUiThread(new Runnable() {
                public void run() {


                    profile_image.setImageDrawable(result);

                }
            });
            progressDialog.dismiss();
        } catch (Exception e) {
            // nothing
        }
    }
Foi útil?

Solução

Picasso allows for hassle-free image loading in your application—often in one line of code!

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);

Many common pitfalls of image loading on Android are handled automatically by Picasso

  • Handling ImageView recycling and download cancelation in an adapter.
  • Complex image transformations with minimal memory use.
  • Automatic memory and disk caching.

enter image description here

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top