2011-01-06 114 views
2

我正在使用Android 2.2。我正在尝试下面的代码来运行:setImageUri无法正常工作

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

让我给你一个方法 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://临时/文件/书籍/images/1325310017.jpg - >(这是你的图片网址)

0

你不能在这样的HTPP访问图像。您需要先将图像下载到文件,流或变量中。

从这个话题:Android image caching

我使用的示例与高速缓存,并导致位图:

URL url = new URL(strUrl); 
URLConnection connection = url.openConnection(); 
connection.setUseCaches(true); 
Object response = connection.getContent(); 
if (result instanceof Bitmap) { 
    Bitmap bitmap = (Bitmap)response; 
}