我正在開發一個應用程序,它使用WiFi將桌面的屏幕圖像傳輸到Android設備。我可以將圖像作爲字節數組從PC傳輸到Android設備。但無法查看圖像在Android的device.Following ImageView的成分是兩段代碼從服務器(PC)和客戶端(Android設備):在ImageView中加載圖像時出現
服務器:
DataOutputStream ds=new DataOutputStream(soc.getOutputStream());
Rectangle rect= new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
Robot rb=new Robot();
img=rb.createScreenCapture(rect);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
ImageIO.write(img, "PNG",baos);
byte[] image=baos.toByteArray();
baos.flush();
baos.close();
System.out.println(image.length);
ds.writeInt(image.length);
ds.write(image);
ds.flush();
ds.close();
客戶:
DataInputStream bis=new DataInputStream(csoc.getInputStream());
int imgsize=bis.readInt();
byte[] img=new byte[imgsize];
int m;
while((m=bis.read())!=-1){
bis.read(img,0,img.length);
}
bis.close();
BitmapFactory.Options op=new BitmapFactory.Options();
op.inSampleSize=8;
Bitmap bmpim=BitmapFactory.decodeByteArray(img,0,img.length);
imv.setImageBitmap(BitmapFactory.decodeByteArray(img,0,img.length,op));//imv is ImageView object
'調試和Trace' – RobinHood