I am using this code to get my image from facebook server.
String imageURL = "http://graph.facebook.com/"+id+"/picture?type=large";
InputStream inputStream = new URL(imageURL).openConnection().getInputStream();
DataInputStream dataStream = new DataInputStream(inputStream);
byte[] dataBuffer = new byte[4096];
ByteArrayOutputStream dataHolder = new ByteArrayOutputStream();
int n;
while ( (n=dataStream.read(dataBuffer))!=-1 ){
dataHolder.write(dataBuffer, 0, n);
}
and using the same way to get my image from my server:
FileInputStream inputStream = new FileInputSTream("/image.jpg");
DataOutputStream dataStream = new DataOutputStream(socket.getOutpustStream());
byte[] dataBuffer = new byte[4096];
i开发者_运维技巧nt n;
while ( (n=inpuStream.read(dataBuffer))!=-1 ){
dataStream.write(dataBuffer, 0, n);
}//get -1 perfectly
and getting them on android app like this:
DataInputStream dataStream = new DataInputStream(socket.getInputStream());
byte[] dataBuffer = new byte[4096];
ByteArrayOutputStream dataHolder = new ByteArrayOutputStream();
int n;
while ( (n=dataStream.read(dataBuffer))!=-1 ){
dataHolder.write(dataBuffer, 0, n);
}//-1 ??????????????
but the android app cant get the right file and it cant go out form while loop,exactly can not get -1 but yesterday I accidently got -1 from some changes I did,really can not understand why I can not get -1 in my android app however I can get it from the servers while loop
You can directly convert the inputstream to a bitmap instead of reading bytes.
Bitmap myBitmap = BitmapFactory.decodeStream(inputStream);
Here is just some glassball guessing: Did you close the stream on the server side?
Put a dataStream.close();
after your writing loop.
精彩评论