2016-11-14 34 views
-1

我如何轉換的圖像(URL)立足64:轉換圖像與URL轉化基地64 +安卓

我嘗試以下,但它並沒有在工作

InputStream inputStream = null; 
try { 
    inputStream = new BufferedInputStream(
      new FileInputStream("https://blahblah/xyz.png")); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 

byte[] bytes; 
byte[] buffer = new byte[8192]; 
int bytesRead; 
ByteArrayOutputStream output = new ByteArrayOutputStream(); 

try { 
    while ((bytesRead = inputStream.read(buffer)) != -1) { 
     output.write(buffer, 0, bytesRead); 
    } 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
bytes = output.toByteArray(); 
String encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT); 

遇到錯誤先試試看。任何人都可以建議,可以做些什麼?

使用new URL("https://blahblah/xyz.png").openStream(),它崩潰與以下錯誤:

android.os.NetworkOnMainThreadException 
+1

你也可以請出版堆棧跟蹤? – Sid

+1

'image(含網址)'????什麼是帶有網址的圖片? – greenapps

+0

請在這裏添加錯誤。 –

回答

0

首先下載從指定網址的圖像,然後使用位圖轉換成字節數組,然後執行Base64編碼。