2011-01-06 112 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; 
}