2011-11-29 10 views
5

Quando tento di caricare un'immagine da un URL, utilizzando il seguente codice (percorso dell'immagine reale rimosso):Errore durante la lettura dal png.class

Bitmap bitmap = BitmapFactory.decodeStream((InputStream)new URL("http://some-path/img.png").getContent()); 

ricevo il seguente errore:

Error reading from ./org/apache/harmony/awt/www/content/image/png.class 

Qualche idea su cosa potrebbe causare l'errore?

Sto utilizzando un AVD di GoogleTV, se è importante.

+0

è il codice di risposta del server 200 ? – rekire

+0

Sì. Ho confermato che gli URL sono validi. – Steve

+0

Ho usato uno sniffer di pacchetti solo per essere sicuro, e le richieste di immagine sono state fatte, e viene restituita una risposta di 200 con l'immagine. – Steve

risposta

4

Spero che questo sia sufficiente.

Se si utilizza php;

echo base64_encode($imgBinary); // You can get the imagebinary by using the fread and fopen methods provided by php 

su Android:

HttpClient client = new DefaultHttpClient(); 
HttpResponse response = client.execute(new HttpGet(url)); 
HttpEntity entity = httpResponse.getEntity(); 

if(entity != null) { 
InputStream is = entity.getContent(); 
byte[] decodedString = Base64.decode(convertStreamToString(is), Base64.DEFAULT); 
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
} 

Questo non è probabilmente il modo più efficiente, ma dovrebbe fare il lavoro. Da lì in poi puoi costruire :)

Puoi comprimere la bitmap in un PNG dopo e salvarla. esempio:

decodedByte.compress(compressformat, quality, stream);//suported compress formats can be used like so: Bitmap.CompressFormat.PNG etc 

convertStreamToString sono metodi facilmente reperibili. Fai una rapida ricerca su google o scrivi la tua.

2

provare questo metodo: il suo lavoro per me Ciò restituisce bitmap

bmp=getBitmapFromURL(ur url here); 

scrittura questo metodo

public static Bitmap getBitmapFromURL(String src) { 
     try { 

      URL url = new URL(src); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 
      Bitmap mybitmap = BitmapFactory.decodeStream(input); 

      return mybitmap; 

     } catch (Exception ex) { 

      return null; 
     } 
0

Per quanto ho sperimentato lo scenario come la vostra che ho trovato che il flusso di ingresso per un l'immagine non può essere ottenuta con i metodi semplici di ottenere stream prova ad aggiornare le seguenti cose nel tuo codice e quindi verifica i risultati. Sono sicuro che otterrai quello che vuoi.

Bitmap bitmap = BitmapFactory.decodeStream(getBitmapStream("http://some-path/img.png")); 

e qui è il metodo che può essere dichiarata all'interno della classe per essere chiamato direttamente nel tuo metodo di flusso di decodifica

public InputStream getBitmapStream (String url) 
{ 
    HttpGet httpRequest = null; 
    InputStream instream=null; 
    try { 
     URL bitmapUrl=new URL(url); 
      httpRequest = new HttpGet(bitmapUrl.toURI()); 
      HttpClient httpclient = new DefaultHttpClient(); 
      HttpResponse response = (HttpResponse) httpclient.execute 
    (httpRequest); 

      HttpEntity entity = response.getEntity(); 
      BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity 
    (entity); 
      instream = bufHttpEntity.getContent(); 
    } catch (URISyntaxException e) { 
      e.printStackTrace(); 
    } catch (MalformedURLException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 


    return instream; 
} 
0

La mia ipotesi sarebbe

URL url = new URL("some url path"); 
URLConnection urlConnection = url.openConnection(); 
BitmapDrawable image = new BitmapDrawable(urlConnection.getInputStream()); 
Problemi correlati