Pergunta

Estou usando o Android 2.2. Estou tentando seguir o código para executar:

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

Mas a imagem não é visível na minha tela Android.

Sugerir algo.

Atenciosamente, Rahul

Foi útil?

Solução

Deixe -me dar -lhe um método utilia

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!!!");
    }
}

}

quando adaptador para usar

assim

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

//http://temp/file/book/images/1325310017.jpg -> (esta é a sua imagem url ..)

Outras dicas

Você não pode acessar uma imagem sobre HTPP assim. Você precisará baixar sua imagem em um arquivo, fluxo ou variável primeiro.

A partir deste tópico: Cache de imagem Android

Eu usei o exemplo com cache e resultando em um 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;
} 

Eu usei a solução fornecida aqui:

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

Atenciosamente, Rahul

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top