Reputation: 551
I want to get the photo from internet so I use setImageURI, but it seems can't be done, but if I use setImageResource(R.drawable.) under the same function, it works... How could I fix the setImageURI's error?
public View getView(int position, View convertView, ViewGroup parent) {
ImageView i = new ImageView(mContext);
//this is working
int p = R.drawable.fb;
i.setImageResource(p);
//this is not working
i.setImageURI(Uri.parse("http://www.vaultads.com/wp-content/uploads/2011/03/google-adsense.jpg"));
return i;
}
Upvotes: 0
Views: 15437
Reputation: 28093
wan to get the photo from internet so i use setImageURI, but it seems cant be done, but while if i use setImageResource(R.drawable.)
The most most important thing
setImageResource is synchronous so it will execute correctly but setImageURI from URL is asynchronous operation and it must be performed in separate thread than UI thread
Following Snippet will help you.
new Thread() {
public void run() {
try {
url = new URL("http://www.vaultads.com/wp-content/uploads/2011/03/google-adsense.jpg");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
image = BitmapFactory.decodeStream(url.openStream());
} catch (IOException e) {
e.printStackTrace();
}
runOnUiThread(new Runnable() {
@Override
public void run() {
i.setImageBitmap(image);
}
});
}
}.start();
In case that too don't work then You have three other options
Option1
URL myUrl = new URL("http://www.vaultads.com/wp-content/uploads/2011/03/google-adsense.jpg");
InputStream inputStream = (InputStream)myUrl.getContent();
Drawable drawable = Drawable.createFromStream(inputStream, null);
i.setImageDrawable(drawable);
Option2
try {
Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://www.vaultads.com/wp-content/uploads/2011/03/google-adsense.jpg").getContent());
i.setImageBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Option3
public static Bitmap loadBitmap(String url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e(TAG, "Could not load Bitmap from: " + url);
} finally {
closeStream(in);
closeStream(out);
}
return bitmap;
}
i.setImageBitmap(loadBitmap("http://www.vaultads.com/wp-content/uploads/2011/03/google-adsense.jpg"));
Upvotes: 1
Reputation: 7478
That image URL doesn't work for me.
I would recommend going another route for downloading an image from the internet. If you call setImageURI
from getView
it will be running on the UI thread, which will cause your app to freeze until the image is returned from the network. Also, the image won't be cached so you could potentially download the same image over and over.
Check out this ImageLoader library for Android which can simplify downloading images. It handles downloading them in the background, it can process multiple request simultaneously, and it caches images for you.
Upvotes: 0