2010-12-22 25 views
11

我想知道是否有可能使用BitmapFactory.decodeFile方法從http位置解碼圖像?是否可以使用BitmapFactory.decodeFile方法解碼來自http位置的圖像?

例如,

ImageView imageview = new ImageView(context); 
Bitmap bmp = BitmapFactory.decodeFile("http://<my IP>/test/abc.jpg"); 
imageview.setImageBitmap(bmp); 

但是bmp總是返回null。

是否有任何其他方式來實現這種情況下,我有我的服務器電腦中的一組圖像,我通過xml將圖像加載到我的畫廊應用程序?

感謝,

+0

我希望,你得到Skia的解碼器返回false,請確保您的收益率這個消息還是不行,請檢查您的logcat,此消息???? – 2010-12-22 14:10:30

+0

@Sankar:雅,我檢查了logcat,我是__NOT__「得到Skia解碼器返回false」的消息。 – Sen 2010-12-22 14:31:40

+0

然後告訴我你的Logcat中有什麼信息? – 2010-12-22 14:32:30

回答

10

@Amir & @Sankar:感謝您的寶貴建議。

我做了下面的代碼片段解決了上述問題:

ImageView iv = new ImageView(context); 

try{ 
    String url1 = "http://<my IP>/test/abc.jpg"; 
    URL ulrn = new URL(url1); 
    HttpURLConnection con = (HttpURLConnection)ulrn.openConnection(); 
    InputStream is = con.getInputStream(); 
    Bitmap bmp = BitmapFactory.decodeStream(is); 
    if (null != bmp) 
     iv.setImageBitmap(bmp); 
    else 
     System.out.println("The Bitmap is NULL"); 

} catch(Exception e) { 
} 

感謝,

16

使用decodeStream並通過URL的輸入流來代替。

下面是一個例子:

Bitmap bmp = BitmapFactory.decodeStream(new java.net.URL(url).openStream()) 
5
String urldisplay="http://www.google.com/";//sample url 
Log.d("url_dispaly",urldisplay); 
try{  
InputStream in = new java.net.URL(urldisplay).openStream(); 
Bitmap mIcon11 = BitmapFactory.decodeStream(new SanInputStream(in)); 
} 
catch(Exception e){} 

創建的類名SanInputStream

public class SanInputStream extends FilterInputStream { 
     public SanInputStream(InputStream in) { 
     super(in); 
     } 
     public long skip(long n) throws IOException { 
     long m = 0L; 
     while (m < n) { 
      long _m = in.skip(n-m); 
      if (_m == 0L) break; 
      m += _m; 
     } 

     return m; 
     } 
} 
2

如果我沒有記錯,@Sen代碼段應在以下情況下返回null。 BMP文件和logcat應該記錄:

skia decoder->decode returned false 

如果發生這樣的事情,請嘗試使用此代碼(也適用於bitm AP輸入):

HttpGet httpRequest = null; 

try { 
    httpRequest = new HttpGet(url.toURI()); 
} catch (URISyntaxException e) { 
    e.printStackTrace(); 
} 

HttpClient httpclient = new DefaultHttpClient(); 

HttpResponse response = (HttpResponse) httpclient.execute(httpRequest); 

HttpEntity entity = response.getEntity(); 

BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 

InputStream instream = bufHttpEntity.getContent(); 

bmp = BitmapFactory.decodeStream(instream); 

Source

0

//爲的AsyncTask

的子類
GetXMLTask task = new GetXMLTask(); 

//執行任務

task.execute(new String[] { "ImageURL" }); 

//然後在Asyntask類創建一個對象將圖像分配給圖像視圖以避免android.os.NetworkOnMainThreadException

private class GetXMLTask extends AsyncTask<String, Void, Bitmap> { 
    @Override 
    protected Bitmap doInBackground(String... urls) { 
     Bitmap map = null; 
     for (String url : urls) { 
      map = downloadImage(url); 
     } 
     return map; 
    } 

    // Sets the Bitmap returned by doInBackground 
    @Override 
    protected void onPostExecute(Bitmap result) { 
     imageView.setImageBitmap(result); 
    } 

    // Creates Bitmap from InputStream and returns it 
    private Bitmap downloadImage(String url) { 
     Bitmap bitmap = null; 
     InputStream stream = null; 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inSampleSize = 1; 

     try { 
      stream = getHttpConnection(url); 
      bitmap = BitmapFactory. 
        decodeStream(stream, null, bmOptions); 
      stream.close(); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return bitmap; 
    } 

    // Makes HttpURLConnection and returns InputStream 
    private InputStream getHttpConnection(String urlString) 
      throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    } 
} 
相關問題