Socket recieves Images with delay of 1 - android

I have a socket that recieves images via one InputStream that doesnt get closed. I want to send images continiously that way. But now the images get recieved with a delay of 1 image (the first one after I sent the second one, the second one after I sent the third one, ....). What am I doing wrong?
Server
public static void readImages(InputStream stream) throws IOException {
stream = new BufferedInputStream(stream);
BufferedImage image = null;
int j = 0;
while (true) {
stream.mark(MAX_IMAGE_SIZE);
ImageInputStream imgStream = ImageIO.createImageInputStream(stream);
Iterator<ImageReader> i = ImageIO.getImageReaders(imgStream);
if (!i.hasNext()) {
System.out.println("No more image readers");
break;
}
ImageReader reader = i.next();
reader.setInput(imgStream);
image = reader.read(0);
ImageIO.write(image,"jpg",new File("current" + j + ".jpg"));
System.out.println("Save an image " + j);
if (image == null) {
System.out.println("Image is null");
break;
}
long bytesRead = imgStream.getStreamPosition();
stream.reset();
stream.skip(bytesRead);
j++;
}
}
Client
new Thread(new Runnable() {
#Override
public void run() {
try {
OutputStream outputStream = server.getOutputStream();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmapToSend = Bitmap.createScaledBitmap(bitmapToSend, 900, 800, true);
bitmapToSend.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
outputStream.write(byteArray);
outputStream.flush();
} catch (IOException e) {
System.out.println("Socket not created");
e.printStackTrace();
}
}
}).start();
Note I dont close the output stream of the client, so I can send pictures all the time.

Using ImageIO.getImageReaders(imgStream) doesn't seem logical for socket streams, since it probably expects all images to be available at once. That may be the reason of your delay.
Secondly, for decompressing images there is a simple method BitmapFactory.decodeStream().
Thirdly, since client already creates a "JPG" format, the server just needs to store it. You only need to send at start the number of bytes and zero after all files have been sent.
Client:
new Thread(new Runnable() {
#Override
public void run() {
try {
ByteArrayOutputStream memoryStream = new ByteArrayOutputStream();
Bitmap bitmapToSend =
Bitmap.createScaledBitmap(bitmapToSend, 900, 800, true);
bitmapToSend.compress(
Bitmap.CompressFormat.JPEG,100, memoryStream);
byte[] byteArray = memoryStream.toByteArray();
memoryStream = null;
DataOutputStream outputStream =
new DataOutputStream(server.getOutputStream());
outputStream.writeInt(byteArray.length);
outputStream.write(byteArray);
outputStream.flush();
} catch (IOException e) {
System.out.println("Socket not created");
e.printStackTrace();
}
}
}).start();
Server:
public static void readImages(InputStream stream) {
DataInputStream imgInput = new DataInputStream(stream);
int index = 0;
int byteLength;
try {
while ((byteLength = imgInput.readInt())>0) {
byte[] buffer = new byte[byteLength];
imgInput.readFully(buffer);
OutputStream imgOutput = new FileOutputStream("current" + (index++) + ".jpg");
imgOutput.write(buffer);
imgOutput.close();
}
} catch (IOException ex) {
// .............
} finally {
try {
imgInput.close();
} catch (IOException ex1) {
//...........
}
}
}

Related

Interrupt download which uses dataInputStream

Have a pretty simple code for download file.
class Downloader {
private static Downloader Dinstance = null;
private boolean isAborted = false;
void setAborted(boolean isAborted) {
isAborted = true;
}
int getAborted() {
return isAborted;
}
static Downloader getInstance(#NonNull Context context) {
if (Dinstance == null)
Dinstance = new Downloaderr(context.getApplicationContext());
return Dinstance;
}
private Downloader() {
...
}
function download() {
...
try {
URL url = new URL(RequestedURL);
InputStream inputStream = url.openStream();
DataInputStream dataInputStream = new DataInputStream(inputStream);
FileOutputStream fileOutputStream = new FileOutputStream(FileName);
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = dataInputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, bytesRead);
if (getAborted()) {
Log.d(static_variables.TAG, "Aborting 1");
fileOutputStream.close();
Log.d(static_variables.TAG, "Aborting 2");
dataInputStream.close();
Log.d(static_variables.TAG, "Aborting 3");
inputStream.close();
Log.d(static_variables.TAG, "All closed");
file.delete();
downloadFinished = true;
Log.d(static_variables.TAG, "Returning");
return false;
}
fileOutputStream.close();
dataInputStream.close();
inputStream.close();
} catch (IOException e) {
downloadFinished = true;
return false;
}
...
}
}
In MainActivity
First button listener(start download):
function onClick() {
new Thread(new Runnable() {
#Override
public void run() {
Downloader.getInstance().download(...);
}
}).start();
}
Second button listener:
function onClick() {
Downloader.getInstance().setAborted(true);
}
Download going in thread.
In logs time for dataInputStream.close(); is biggest. And other research shows that streams won't close before file will be fully downloaded.
How I can terminate InputStream in progress?
During research I found two things.
Download aborts with this approach, but there is a buffer and for small files(songs, in my case) it's almost untrackable. For 500Mb file download, when you press abort on 15Mb downloaded, then after 16-17 it will stop.
If you want(as I am) terminate download faster, then you should use another approach. When starting Thread, save pointer int variable.
Thread threadDownload = new Thread(...);
threadDownload.start();
then abort using
threadDownload.interrupt();
Beware. You'll need remove temporary file by yourself. File which you use in OutputStream.
You also will have some buffer overhead, but it will be not 1-2Mb, but 200-300Kb.

Unable to transfer bigger size files over wi-fi hotspot in Android

I want to transfer a file on Socket connection using Wi-Fi Hotspot IP address and MAC address between two android devices.I am able to transfer a text file with less than 1 KB size but unable to send other extension files and of bigger size using socket. Below is the code for Sender side:-
Socket socket = null;
File file = new File(
Environment.getExternalStorageDirectory(),
"test.mp3");
byte[] bytes = new byte[(int) file.length()];
BufferedInputStream bis;
try {
socket = new Socket(dstAddress, dstPort);
bis = new BufferedInputStream(new FileInputStream(file));
bis.read(bytes, 0, bytes.length);
OutputStream os = socket.getOutputStream();
os.write(bytes, 0, bytes.length);
os.flush();
if (socket != null) {
socket.close();
}
final String sentMsg = "File Sent.....";
((Activity)context_con).runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(context_con,
sentMsg,
Toast.LENGTH_LONG).show();
}});
}catch (ConnectException e) {
e.printStackTrace();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (socket != null) {
socket.close();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
This is the code for Receiver End:-
try {
File file = new File(
Environment.getExternalStorageDirectory(),
"test.mp3");
byte[] bytes = new byte[1024];
InputStream is = socket.getInputStream();
FileOutputStream fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
int bytesRead = is.read(bytes, 0, bytes.length);
bos.write(bytes, 0, bytesRead);
bos.close();
socket.close();
final String sentMsg = "File Received...";
Main.this.runOnUiThread(new Runnable() {
#Override
public void run() {
Toast.makeText(Main.this,
sentMsg,
Toast.LENGTH_LONG).show();
}});
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
try {
socket.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
I want to transfer bigger size files like mp3 file but it only creating 1Kb size file on receiver end not with the exact size which is 2.1 MB. Please help me where I am wrong in this implementation.
Put together this but haven't tested it. This should give some hints
//Server side snippet
public void server(int port) throws IOException {
try (ServerSocket serverSocket = new ServerSocket(port); Socket socket = serverSocket.accept()) {
try (InputStream in = socket.getInputStream(); OutputStream out = new FileOutputStream("test.mp3")) {
byte[] bytes = new byte[2 * 1024];
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
}
}
}
//client side
public static void client(String dstAddress, int dstPort) throws IOException {
try (Socket socket = new Socket(dstAddress, dstPort)) {
File file = new File(Environment.getExternalStorageDirectory(), "test.mp3");
// Get the size of the file
long length = file.length();
if (length > 0) {
byte[] bytes = new byte[2 * 1024];
InputStream in = new FileInputStream(file);
OutputStream out = socket.getOutputStream();
int count;
while ((count = in.read(bytes)) > 0) {
out.write(bytes, 0, count);
}
out.close();
in.close();
}
}
}
You could choose to wrap the resources with try-catch as i've done or you can choose not to. Consider adjusting the buffer size accordingly. Consider this
Please try that.

Proper strategy of getting byte content of big images from Gallery when OutOfMemoryError is thrown

First of all I would like to say, that loading big images strategy is described here. I am aware of the obligation to resize the image to omit OutOfMemoryError. I would like to get bytes from the image to be able to send it through Internet.
public byte[] getAttachment(Context context, String fullFilePath) {
byte[] fileBytes = mFileUtil.readFileAsBytes(fullFilePath);
if (fileBytes == null) {
Logger.i("Unable to get bytes, trying through content resolver");
try {
fileBytes = mFileUtil.readFileFromContentResolver(context, fullFilePath);
} catch (OutOfMemoryError e) {
Uri imageUri = Uri.parse(fullFilePath);
if (checkIfFileIsImage(context, imageUri)) {
try {
InputStream stream = context.getContentResolver().openInputStream(imageUri);
if (stream == null) {
return null;
}
BitmapFactory.Options options = getOptions(stream, 2000, 2000);
stream.close();
stream = context.getContentResolver().openInputStream(imageUri);
if (stream == null) {
return null;
}
Bitmap bitmap = BitmapFactory.decodeStream(stream, null, options);
bitmap = rotateBitmap(context, imageUri, bitmap);
stream.close();
fileBytes = convertBitmapToArray(bitmap);
} catch (Exception e1) {
Logger.e("Unable to get bytes using fallback method, attachment " +
"will not be added");
} catch (OutOfMemoryError e2) {
Logger.e("Unable to get bytes using fallback method, because " +
"attachment is too big. Attachment will not be added");
}
}
System.gc();
}
}
return fileBytes;
}
FileUtil.class
public byte[] readFileAsBytes(String fileName) {
File originalFile = new File(fileName);
byte[] bytes = null;
try {
FileInputStream fileInputStreamReader = new FileInputStream(originalFile);
bytes = new byte[(int) originalFile.length()];
fileInputStreamReader.read(bytes);
} catch (IOException e) {
}
return bytes;
}
public byte[] readFileFromContentResolver(Context context, String fileName) {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream is = null;
InputStream inputStream = null;
try {
inputStream = context.getContentResolver().openInputStream(Uri.parse(fileName));
is = new BufferedInputStream(inputStream);
byte[] data = new byte[is.available()];
is.read(data);
bos.write(data);
} catch (Exception e) {
} finally {
try {
if (is != null) {
is.close();
}
if (inputStream != null) {
inputStream.close();
}
} catch (IOException e1) {
}
}
return bos.toByteArray();
}
As you can see the aim of this code is to get byte[] from Bitmap unlike here. It works without problems in almost any case. However it is especially error prone on low-end devices with older Android systems (but also very rarely).
I don't like the idea of setting largeHeap inside AndroidManifest.xml as this is just masking the problem rather than cope with it. I also would not like to send even smaller images.
Could this piece of code be improved in any other way in order to get rid of OutOfMemoryError?

how to read bytes from a video file in android

I have a question that I want to read bytes from video resided in sdcard in chunk size 1024,
means I have to read 1024 bytes from the file at a time. I am able to fetch number of bytes from the video but I can't get it in chunks, I don't know how to achieve this. Please suggest me the right solution regarding the same.
Thanks in advance.
import java.io.*;
public class FileUtil {
private final int BUFFER_SIZE = 1024;
public void readFile(String fileName) {
BufferedInputStream in = null;
try {
in = new BufferedInputStream(new FileInputStream(fileName));
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
byte[] buffer = new byte[BUFFER_SIZE];
try {
int n = 0;
while ((n = in.read(buffer, 0, BUFFER_SIZE)) > 0) {
/* do whatever you want with buffer here */
}
}
catch(Exception e) {
e.printStackTrace();
}
finally { // always close input stream
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
Based on the code from http://www.xinotes.org/notes/note/648/

Why an application works during debugging but doesn't work on run?

I have a client in Android and server in C#, they communicate through socket.
I have this problem - if I run my client app in debug mode and place a breakpoint in right place - it works perfectly, but without it it doesn't.
Client sends adress of an image to server, server makes a thumbnail of it, converts it to byte[] and sends it back. Client gets the byte[], converts it back into image and shows it.
I've also found out that when it doesn't get the right byte[] its size is 2896 and sometimes 1448, no matter what the original size of the array sent was.
Here's the client:
private void connectSocket(String a){
try {
InetAddress serverAddr = InetAddress.getByName("192.168.1.2");
Socket socket = new Socket(serverAddr, 4444);
String message = a;
flag = 0;
if(a.indexOf(".jpg")>0){
flag = 1;
}
ListItems.clear();
if(!a.equalsIgnoreCase("get_drives"))){
//.....
}
if(!ListItems.isEmpty()){
if(ListItems.get(0).matches("^[A-Z]{1}:$")){
ListItems.set(0, ListItems.get(0)+"\\");
}
}
PrintWriter out = null;
BufferedReader in = null;
InputStream is = null;
try {
out = new PrintWriter( new BufferedWriter( new OutputStreamWriter(socket.getOutputStream())),true);
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
is = socket.getInputStream();
out.println(message);
String text = "";
String finalText = "";
if(flag!=1){
while ((text = in.readLine()) != null) {
finalText += URLDecoder.decode(text, "UTF-8");
}
String[] result = finalText.split("#");
for(int i = 0; i<result.length; i++)
ListItems.add(result[i]);
}
if(flag ==1){
byte[] buffer = new byte[9999];
//placing breakpoint at the next line or before it makes it work fine
int size = is.read(buffer);
//but placing a breakpoint at the line below doesn't make it work
//it starts getting another byte array
byte[] bufffer2 = new byte[size];
for(int g = 0; g<size;g++){
bufffer2[g] = buffer[g];
}
//is.read(bufffer2);
//int read = is.read(buffer);
image = (ImageView)findViewById(R.id.imageView1);
//while ((text = in.readLine()) != null) {
// byte[] b = in.readLine().getBytes();
Bitmap bmp=BitmapFactory.decodeByteArray(bufffer2,0,bufffer2.length);
image.setImageBitmap(bmp);
//}
}
adapter.notifyDataSetChanged();
} catch(Exception e) {
Log.e("TCP", "S: Error", e);
} finally {
socket.close();
}
} catch (UnknownHostException e) {
Log.e("TCP", "C: UnknownHostException", e);
e.printStackTrace();
} catch (IOException e) {
Log.e("TCP", "C: IOException", e);
e.printStackTrace();
}
}
Here's the server:
public class serv {
public static void Main() {
try {
IPAddress ipAd = IPAddress.Parse("192.168.1.2");
TcpListener myList=new TcpListener(ipAd,4444);
m:
myList.Start();
Socket s=myList.AcceptSocket();
byte[] b=new byte[100];
int k=s.Receive(b);
char cc = ' ';
string test = null;
Console.WriteLine("Recieved...");
for (int i = 0; i < k-1; i++)
{
Console.Write(Convert.ToChar(b[i]));
cc = Convert.ToChar(b[i]);
test += cc.ToString();
}
string[] response = null;
ASCIIEncoding asen = new ASCIIEncoding();
switch (test)
{
default:
MyExplorer(test, s);
break;
}
s.Close();
myList.Stop();
goto m;
}
catch (Exception e) {
Console.WriteLine("Error..... " + e.StackTrace);
}
}
public static void MyExplorer(string r, Socket s){
Image imgThumb = null;
if (r.Contains(".jpg"))
{
Image image = null;
image = Image.FromFile(r);
// Check if image exists
if (image != null)
{
imgThumb = image.GetThumbnailImage(100, 100, null, new IntPtr());
s.Send(imageToByteArray(imgThumb));
byte[] b = imageToByteArray(imgThumb);
}
return;
}
}
public static byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
The read(byte []) method in InputStream reads as many bytes as currently available. It does not necessarily mean that there won't be more bytes available later. So you need to do something like
while(true) {
int s = is.read(buffer);
if(s == -1) break;
// else
add read buffer to image array
}
extract image from image array
where "image array" is some byte array where you keep adding the read buffer until you reach then end of the stream.
The reason your code works with the breakpoint is that by the time you go through the debugger the whole stream is available, while in non-debug the whole stream is not yet available when you do the read.
The difference is likely due to the fact that on the debugger, you are able to slow and stop the application - this gives time for the server to respond to your call. It looks like you are not using any asynchronous methods to call connect socket (well, I can't see any evidence of this from your code anyway).
To fix this, you should try extending the AsyncTask object examples here
EDIT: #Carsten probably has the correct solution, but you should STILL use an AsyncTask if you are not already.

Categories

Resources