2011-01-06 8 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(); 

画像は私のアンドロイド画面に表示されません。

は何かを提案します。

よろしく、 ラーフル

答えて

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 - >(これはあなたの画像URLです)

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; 
} 
関連する問題