2016-08-24 40 views
-1
URL imageUrl = new URL("http://192.168.0.103:8080/mm/showImage?id=2&fileName=fesc1cc.JPG&imgType=books"); 

     //URL imageUrl = new URL(url); 
     HttpURLConnection conn = (HttpURLConnection) imageUrl 
       .openConnection(); 

     BufferedReader in = new BufferedReader(
       new InputStreamReader(conn.getInputStream())); 
     StringBuffer buffer = new StringBuffer(); 
     String inputLine; 
     while ((inputLine = in.readLine()) != null) 
      buffer.append(inputLine); 
     in.close();  


     conn.getResponseCode(); 
     System.out.println(conn.getResponseCode()); 
     System.out.println(buffer.toString()); 
     conn.disconnect(); 

如何將我接收的數據轉換爲位圖並將其顯示在圖像視圖中。如何將原始數據轉換爲位圖?

+0

的可能的複製[如何字節數組位圖轉換(http://stackoverflow.com/questions/7620401/how-to-convert-byte-array-to位圖) –

回答

2
byte[] imageAsBytes = Base64.decode(myImageData.getBytes()); 
Bitmap bp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length); 

OR

byte []bt = buffer.toString().getBytes(); 
Bitmap i = BitmapFactory.decodeByteArray(buffer.getBytest or buffer.tobytes, 0,bt.length); 
+0

該數據是在StringBuffer緩衝區中,是否必須將其轉換爲字符串。@ SuhasBachewar –

+0

@ShwetabhSingh您必須編寫「buffer.toString()」或直接傳遞緩衝區數據,如更新後的答案 –

+0

中提到的是什麼bt .length here @SuhasBachewar –

0

你可以用畢加索庫來做到這一點Picasso.with(context).load(imageUrl).into(imageview);

+1

請給你的答案添加一些解釋。在SO上不鼓勵使用僅有代碼的答案。 –

0

使用畢加索庫,而不是。畢加索它很容易使用。 Picasso.with(context).load("url").into(ImageView)。 更多的信息在這裏 http://square.github.io/picasso

-1

更有效的方式是使用Glide。 Glide是一個快速高效的開源Android媒體管理和圖像加載框架,將媒體解碼,內存和磁盤緩存以及資源池整合到一個簡單易用的界面中。

使用庫:

  • 添加以下代碼到你的gradle這個文件(模塊:APP)

    repositories { 
        mavenCentral() 
    } 
    
    dependencies { 
        ... 
        compile 'com.github.bumptech.glide:glide:3.7.0' 
    } 
    
  • 使用滑翔加載您的網址圖像的ImageView

    Glide.with(this).load(imageUrl).into(imageView); 
    

欲瞭解更多信息請訪問https://github.com/bumptech/glide

祝你好運。

編輯:

Glide.with(this) 
.load("http://192.168.0.103:8080/mm/showImage?id=2&fileName=fesc1cc.JPG&imgType=books") 
.into(imageView); 
+0

它也轉換原始數據 –

+0

是的,你可以加載圖像的字節數組與Glide,但我想你想從一個url加載圖像?您不需要打開連接並獲取流。 Glide可以處理這個,只需要給它imageUrl和imageview。 –

+0

我必須發送一些參數並從URL獲取原始數據,然後將其轉換爲位圖@BatuhanCoskun –

相關問題