我需要开发一个应用程序,将图像从android连续传输到android,应该在客户端作为视频接收..我有两个应用程序,其中一个(服务器)将通过wifi连接的套接字发送文件其他应该客户端应该重新发送已发送的图像...目前我只是将其保存在客户端的一个位置...我能够正确接收文件...但问题是无法发送所有文件一直正确... 意味着一段时间图像文件将被转移,一段时间我将无法接收,当我将无法接收时,我将获得例外 :java.io.UTFDataFormatException: ...并且文件未写入并保存在接收方...
如果我无法连续接收图像......我可以认为代码中存在一些问题..但是我可以将它转移一段时间......而且有些时候无法转移...我无法想一想问题是什么...... PLZ任何指导
错误是:
11-18 10:38:17.351:W / System.err(1001):java.io.UTFDataFormatException:错误的第二个或第三个字节位于2 11-18 10:38:17.359:W / System.err(1001):at java.nio.charset.ModifiedUtf8.decode(ModifiedUtf8.java:53) 11-18 10:38:17.359:W / System.err(1001):at java.io.DataInputStream.decodeUTF(DataInputStream.java:444) 11-18 10:38:17.359:W / System.err(1001):at java.io.DataInputStream.decodeUTF(DataInputStream.java:438) 11-18 10:38:17.359:W / System.err(1001):at java.io.DataInputStream.readUTF(DataInputStream.java:433)
,当我收到此异常时,文件未保存..
我通过捕获图像,保存和发送测试的许多场景......还压缩图像并发送......在这些场景中,很少有人会发现...我无法弄清楚它...
发件人代码:
文件myFile =新文件(sdCard +“/ image / image.jpg”);
byte [] mybytearray = new byte [(int)myFile.length()];
FileInputStream fis = new FileInputStream(myFile);
BufferedInputStream bis = new BufferedInputStream(fis);
//bis.read(mybytearray, 0, mybytearray.length);
DataInputStream dis = new DataInputStream(bis);
dis.readFully(mybytearray, 0, mybytearray.length);
OutputStream os = socket.getOutputStream();
tv.setText("Send file name size to server");
//Sending file name,file size and to the server
DataOutputStream dos = new DataOutputStream(os);
dos.writeUTF(myFile.getName());
dos.writeLong(mybytearray.length);
dos.write(mybytearray, 0, mybytearray.length);
dos.flush();
socket.close();
tv.setText("Socket Close");
tv.setText("Sent");
收件人代码: File dir = new File(sdCard.getAbsolutePath()+“/ dir1 / dir2”); dir.mkdirs();
String fileName = clientData.readUTF();
File file = new File(dir,fileName);
OutputStream output = new FileOutputStream(file);
long size = clientData.readLong();
byte[] buffer = new byte[1024];
while (size > 0 && (bytesRead = clientData.read(buffer, 0, (int)Math.min(buffer.length, size))) != -1)
{
output.write(buffer, 0, bytesRead);
size -= bytesRead;
System.out.println("Writing");
}
// status.setText("Received");
// Closing the FileOutputStream handle
output.close();
s.close();
谢谢和问候, Divya.K
答案 0 :(得分:0)
我被困在这个东西上,我结合了几天的搜索并达到了这个......尝试一下,看看是否适合你..
Bitmap myBitmap = null;
try {
File imgFile = new File(imgPath);
if (imgFile.exists()) {
File image = new File(imgPath, "imagename.jpg");
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
myBitmap = BitmapFactory.decodeFile(image.getAbsolutePath(),bmOptions);
//myBitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true); //uncomment this if you want to scale the image
//imageView.setImageBitmap(bitmap);//to set bitmap to image view
}
Socket sock = new Socket("192.168.1.1", 80);//ip adress and port number
ByteArrayOutputStream bos = new ByteArrayOutputStream();
if (myBitmap != null) {
myBitmap.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
}
byte[] bitmapdata = bos.toByteArray();
ByteArrayInputStream bs = new ByteArrayInputStream(bitmapdata);
BufferedInputStream bis = new BufferedInputStream(bs);
bis.read(bitmapdata, 0, bitmapdata.length);
OutputStream os = sock.getOutputStream();
System.out.println("Sending...");
os.write(bitmapdata, 0, bitmapdata.length);
os.flush();
sock.close();
} catch (Exception e) {
e.printStackTrace();
}