user3368883
user3368883

Reputation:

Android Loading remote images

In Android, what is the simplest approach to the following:

  1. Load an image from a remote server.
  2. Display it in an ImageView.

I am trying to display a image from a URL in a ImageView, and I tried this method based on http://developer.android.com/reference/android/widget/ImageView.html#setImageURI(android.net.Uri) it doesn't seem to work... I've crash application.

Pics.java

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;

public class Pics extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.pics);

        ImageView imageView = (ImageView) findViewById(R.id.ImageView1);

        Uri imgUri = Uri.parse("http://www.remoteServer.com/img/disconnect.png");
        imageView.setImageURI(null); 
        imageView.setImageURI(imgUri);
    }
}

AndroidManifest.xml

<activity
   android:name=".Pics"
   android:label="test" >
</activity>

Pics.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <imageview
        android:id="@+id/ImageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </imageview>

</LinearLayout>

Upvotes: 0

Views: 1964

Answers (4)

nivensookharan
nivensookharan

Reputation: 193

You could also try this out if you need to cache those images in your app.

Loopj Image Cache

Upvotes: 1

nivensookharan
nivensookharan

Reputation: 193

I think the best way is to do it asynchronously.

I normally start up a progress indicator on page load then trigger off the async process.

On page load

new DownloadImageTask((ImageView)findViewById(R.id.QrCodeImageView)).execute("https://chart.googleapis.com/chart?cht=qr&chs=150x90&chl=HelloWorld");

Async Process

    class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;

    public DownloadImageTask(ImageView bmImage) {
        this.bmImage = bmImage;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(urldisplay).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);

            ProgressBarQrCode.setVisibility(View.GONE);
        } catch (Exception e) {
            Log.e("Error", e.getMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        bmImage.setImageBitmap(result);
    }
}

I hope this helps you

Upvotes: 1

Minas
Minas

Reputation: 1422

Try out this library Android-Universal-Image-Loader

Upvotes: 0

Jos&#233; Barbosa
Jos&#233; Barbosa

Reputation: 903

What's your error?

Well, I use a lib for images called picasso.

You can add images to your view with one line of code:

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

If you are using images in a list probably you need to create a thumb to use less memory:

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

Upvotes: 0

Related Questions