setImageUri is not working
I am using Android 2.2. I am trying following code to run:
Uri uri=Uri.parse("http://bluediamondring-s.com/wp-content/uploads/2010/08/gold-ring.jpg");
File f=new File(uri.getPath());
image.setImageURI(Uri.parse(f.toString()));
image.invalidate();
But image is not visible on my android screen.
suggest s开发者_高级运维omething.
Regards, Rahul
let me give you an method utils
public class AsyncUploadImage extends AsyncTask<Object, Object, Object> {
private static final String TAG = "AsyncUploadImage ";
ImageView iv;
private HttpURLConnection connection;
private InputStream is;
private Bitmap bitmap;
public AsyncUploadImage(ImageView mImageView) {
iv = mImageView;
}
@Override
protected Object doInBackground(Object... params) {
URL url;
try {
url = new URL((String) params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
is = connection.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (connection != null) {
connection.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return bitmap;
}
@Override
protected void onPostExecute(Object result) {
super.onPostExecute(result);
if (null != result) {
iv.setImageBitmap((Bitmap) result);
Log.i(TAG, "image download ok!!!");
}else {
iv.setBackgroundResource(R.drawable.shuben1);
Log.i(TAG, "image download false!!!");
}
}
}
when adapter to use
like this
new AsyncUploadImage(itemIcon).execute("http://temp/file/book/images/1325310017.jpg");
//http://temp/file/book/images/1325310017.jpg -> (this is your image url..)
You cannot access an image over HTPP like that. You'll need to download your image into a file, stream or variable first.
From this topic: Android image caching
I used the example with cache and resulting in a Bitmap:
URL url = new URL(strUrl);
URLConnection connection = url.openConnection();
connection.setUseCaches(true);
Object response = connection.getContent();
if (result instanceof Bitmap) {
Bitmap bitmap = (Bitmap)response;
}
I've used solution given here:
http://asantoso.wordpress.com/2008/03/07/download-and-view-image-from-the-web/
Regards, Rahul
精彩评论