문제

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 something.

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;
} 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top