Вопрос

I need some help. I want to refresh my ImageView loaded from a URL in my android app. I try use timers but doesn't work. Any thoughts?

I have this code in Main Activity:

public class Imagens extends Activity {

private ImageView imagem;
private Bitmap bit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_imagens);
    imagem=(ImageView) findViewById(R.id.imageView);
    bit=getBitmapFromURL("http://www.onlinedegrees.org/wp-content/uploads/android1.jpg");
    imagem.setImageBitmap(bit);
}
public Bitmap getBitmapFromURL(String src){
    try{
        URL url = new URL(src);
        HttpURLConnection connection= (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    }
    catch (Exception e){
        e.printStackTrace();
        return null;
    }
}
}

And in the AndroidManifest uses:

uses-permission android:name="android.permission.INTERNET"

to allow internet connection.

Это было полезно?

Решение

I can recommend a different way that works like a charm: Android Query.

You can download that JAR file from here

AQuery androidAQuery = new AQuery(this);

As an example:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW);

It's very fast and accurate, and using this you can find many more features like animation when loading, getting a bitmap (if needed), etc.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top