2015-01-10 107 views
0

我正在創建一個從java服務器向android客戶端發送圖像。 這裏是我的Android代碼:將位圖圖像設置爲ImageView時出錯

protected Void doInBackground(Void... arg0) { 
try { 
    socket = new Socket("192.168.237.1", 6666); 
      dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
      dataInputStream = new DataInputStream(socket.getInputStream()); 
      // dataOutputStream.writeUTF(textOut.getText().toString()); 
      String base64Code = dataInputStream.readUTF(); 

      Log.d("String", ":" + base64Code); 
      // 
      byte[] decodedString; 
      decodedString = Base64.decode(base64Code); 
      Log.d("Ds",""+decodedString); 
      Log.d("St--", ":" + decodedString.length); 
      BitmapFactory.Options options=new BitmapFactory.Options(); 
      options.inMutable=true; 
      bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options); 
      Drawable ob=new BitmapDrawable(getResources(),bitmap); 
      imageView = (ImageView) findViewById(R.id.imageView1); 
      imageView.setBackgroundDrawable(ob); 
      /*//imageView.setImageBitmap(bitmap); 
      ByteArrayInputStream input=new ByteArrayInputStream(decodedString); 
      bitmap=BitmapFactory.decodeStream(input); 
      imageView.setImageBitmap(bitmap);*/ 
      Log.d("Bitmap",""+bitmap); 
     } catch (UnknownHostException e) { 
       e.printStackTrace(); 
     } catch (Exception e) { 
       e.printStackTrace(); 
       Log.d("Error", "" + e); 
     } 

}

我已經編碼在Java usung阿帕奇共同編解碼器的字節數組,在Android的程序解碼。 我得到的錯誤是NullPointeException在​​。 這段代碼中的錯誤是什麼?

回答

0

你需要做的是這樣的:

private class MyAsyncTask extends AsyncTask<Void, Void, Bitmap> { 

    @Override 
    protected Bitmap doInBackground(Void... params) { 
    try { 
     socket = new Socket("192.168.237.1", 6666); 
     dataOutputStream = new DataOutputStream(socket.getOutputStream()); 
     dataInputStream = new DataInputStream(socket.getInputStream()); 
     // dataOutputStream.writeUTF(textOut.getText().toString()); 
     String base64Code = dataInputStream.readUTF(); 

     Log.d("String", ":" + base64Code); 
     // 
     byte[] decodedString; 
     decodedString = Base64.decode(base64Code); 
     Log.d("Ds",""+decodedString); 
     Log.d("St--", ":" + decodedString.length); 
     BitmapFactory.Options options=new BitmapFactory.Options(); 
     options.inMutable=true; 
     bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length,options); 

     return bitmap; 

     /*//imageView.setImageBitmap(bitmap); 
     ByteArrayInputStream input=new ByteArrayInputStream(decodedString); 
     bitmap=BitmapFactory.decodeStream(input); 
     imageView.setImageBitmap(bitmap);*/ 
     Log.d("Bitmap",""+bitmap); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
     Log.d("Error", "" + e); 
    } 

    return null; 
    } 

    @Override 
    protected void onPostExecute(Bitmap bitmap) { 
    if (bitmap != null) { 
     Drawable ob =new BitmapDrawable(getResources(), bitmap); 
     imageView = (ImageView) findViewById(R.id.imageView1); 
     imageView.setBackgroundDrawable(ob); 
    } else { 
     // error 
    } 
    } 

} 
+0

哇!這工作。非常感謝。 –

0

您可以從後臺線程執行UI操作。他們需要在UI線程中執行。因此,請嘗試執行onPostExecute()中的操作。

+0

如果我做它'OnPostExecute()'它不連接到服務器。如果我在後臺線程連接到服務器,它會給NetworkMainException –

相關問題