setImageUri不起作用

时间:2011-01-06 06:58:19

标签: android imageview

我使用的是Android 2.2。我正在尝试运行代码:

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();

但是在我的Android屏幕上看不到图像。

提出建议。

此致 拉胡

3 个答案:

答案 0 :(得分:3)

让我给你一个方法  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!!!");
    }
}

}

何时使用适配器

像这样

new AsyncUploadImage(itemIcon).execute("http://temp/file/book/images/1325310017.jpg");

//http://temp/file/book/images/1325310017.jpg - &gt; (这是你的图片网址..)

答案 1 :(得分:0)

您无法通过HTPP访问图像。您需要先将图像下载到文件,流或变量中。

从这个主题:Android image caching

我使用了缓存的示例并生成了一个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;
} 

答案 2 :(得分:0)

我使用过这里给出的解决方案:

http://asantoso.wordpress.com/2008/03/07/download-and-view-image-from-the-web/

此致 拉胡