我正在开发一个使用wifi将桌面的屏幕图像传输到android设备的应用程序。我能够将图像作为字节数组从PC传输到Android设备。但是无法在Android设备上的ImageView组件中查看图像以下是来自服务器(PC)和客户端(Android设备)的两段代码:
服务器:
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
答案 0 :(得分:0)
考虑到您的输入流有效,您也可以尝试这样做
DataInputStream bis=new DataInputStream(csoc.getInputStream());
imageView=new ImageView(this);
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
imageView.setAdjustViewBounds(true);
imageView.setImageBitmap(BitmapFactory.decodeStream(bis));
// Add this imageView to existing parent layout
// parentLayout.addView(imageView);