I am writting a app transfer file based Socket.When I transfer file less than 10 mb ,then no matter what happens.But when I transfer file over 20mb, then I get Out of Memory error and crash my app.
This is my code
RandomAccessFile aFile = new RandomAccessFile(path, "r");
FileChannel inChannel = aFile.getChannel();
ByteBuffer buffer = ByteBuffer.allocate(1024);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int n;
while ((n = inChannel.read(buffer)) > 0) {
buffer.flip();
total_length += n;
baos.write(buffer.array(), 0, n);
byte[] aaa = baos.toByteArray();
ObjectStream obstream = new ObjectStream("stream", aaa, session_request, total_length);
aaa=null;
outt.writeObject(obstream);
obstream = null;
baos.reset();
buffer.clear(); // do something with the data and clear/compact it.
}
inChannel.close();
aFile.close();
This is error that geted :
FATAL EXCEPTION: Thread-177
java.lang.OutOfMemoryError
at java.io.ByteArrayOutputStream.toByteArray(ByteArrayOutputStream.java:122)
at com.jsc.Remote2Droid.Thread.ThreadClientData.run(ThreadClientData.java:705)
out of memory at location:
byte[] aaa = baos.toByteArray();
Please help me fix my code.Thank all.Sorry for my english.
You are asking for troubles if you place such big files completely in memory before you send them.
So your approach is wrong.
You only need a small buffer. Then make a loop where you read chuncks from the file in the buffer and then write the bytes in that buffer to the output stream.
In this way it't irrelevant how big the file is.
Related
I try to read a video file in Xamarin android in order to convert it to string64.
I have success with very small files (3 secondes), but if the file is bigger (1 minute) i have an OutOfMemoryError.
I have try to add android:largeHeap="true" android:hardwareAccelerated="false" in the manifest file and modify my code to read the video file but still the issue.
Failed to allocate a 134217744 byte allocation with 25165824 free bytes and 126MB until OOM, max allowed footprint 94159504, growth limit 201326592.
What i have try:
byte[] bytes = await System.IO.File.ReadAllBytesAsync(file.AbsolutePath);
string encoded = Base64.EncodeToString(bytes, 0);
return encoded;
using (var stream = new FileStream(file.AbsolutePath, FileMode.Open, FileAccess.Read))
{
//var b = ReadToEnd(stream);
//encoded = Base64.EncodeToString(b, 0);
//return encoded;
byte[] bytes;
using (var memoryStream = new MemoryStream())
{
stream.CopyTo(memoryStream);
bytes = memoryStream.ToArray();
}
string base64 = Convert.ToBase64String(bytes);
return base64;
}
FileInputStream fis = new FileInputStream(file.AbsolutePath);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
for (int readNum; (readNum = fis.Read(b)) != -1;)
{
bos.Write(b, 0, readNum);
}
byte[] bytes = bos.ToByteArray();
string encoded = Base64.EncodeToString(bytes, 0);
return encoded;
Have you got an idea to fix this or split video file without load it entirely?
Thanks
Do you really have to load it in memory first? Play it directly from the video file path
I am trying to convert audio file to the byte array, but it seems it is not getting converted correctly. I am recording sound using mic, then converting that file to byte array using file's path on the device.
The desired byte array should be like 0x12323
But it is coming like this string [B#14746f6
Below is the code to convert audio to byte array
file is the path of the file on the device. File type is amr
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream out = new ByteArrayOutputStream();
int read = 0;
byte[] buffer = new byte[1024];
while (read != -1) {
read = fis.read(buffer);
if (read != -1)
out.write(buffer,0,read);
}
out.close();
byte[] bytes = out.toByteArray();
Log.e("byte array" ,bytes.toString());
String path= ""; // Audio File path
InputStream is= new FileInputStream(path);
byte[] arr= readByte(is);
Log.e("byte: ",""+ Arrays.toString(arr));
I solved this issue after talking to api guy. I converted byte array to base64 string and passed it. Which resolved the issue.
I need to make API in Json to send the video. I don't want to send the path of video. What is the best way to send the video in JSON which will be used by android and iPhone guys. If I use the base64 or byte[] then I am getting the memory exception error.
File file = new File("video.mp4");
FileInputStream fis = new FileInputStream(file);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
try {
for (int readNum; (readNum = fis.read(buf)) != -1;) {
bos.write(buf, 0, readNum); //no doubt here is 0
System.out.println("read " + readNum + " bytes,");
}
} catch (IOException ex) {
Logger.getLogger(genJpeg.class.getName()).log(Level.SEVERE, null, ex);
}
byte[] bytes = bos.toByteArray();
This is how you add a video byte by byte inside an byte array. You just then send the byte array as JSONOBject by following...
byte[] data; //array holding the video
String base64Encoded = DatatypeConverter.printBase64Binary(data); //You have encoded the array into String
now send that to server. (I am guessing you know how to)..
This is how you will decode your JSON to byteArray again.
byte[] base64Decoded = DatatypeConverter.parseBase64Binary(base64Encoded);
The typical way to send binary in json is to base64 encode it. Java provides different ways to Base64 encode and decode a byte[]. One of these is DatatypeConverter.
I hope it helps.
Cheers!
Edited:
You are getting OutOfMemoryException, because HeapMemory is 2Mb in size and your video is 2Mb, so when inserting into String, it's going out of memory. Even if you put it into an Object instances, you will EITHER have to re-initialize the heap or some way else. I will try to write an answer tomorrow. (Writing this half asleep, might be other way around_
I'm trying to send bmp image using socket. I have such code on android:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
MainActivity.bmp.compress(Bitmap.CompressFormat.JPEG, 20,
stream);
byte[] byteArray = stream.toByteArray();
OutputStream os = echoSocket.getOutputStream();
os.write(byteArray,0,byteArray.length);
os.flush();
and on PC:
String q = SockIn.readLine();
File file = new File("filename.bmp");
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(q);
in bmp file I only get up to 401 bytes, which of course is corrupt bmp image. what am I doing wrong?
MODIFIED
modified PC side, now the code is:
InputStream in_ = clientSocket.getInputStream();
OutputStream out_ = new FileOutputStream("filename.bmp");
final byte[] buffer = new byte[1024];
int read = -1;
int i = 0;
while ((read = in_.read(buffer)) != -1) {
out_.write(buffer, 0, read);
System.out.println(i);
i++;
}
in_.close();
out_.close();
System.out.println("Done");
It never gets to last line( println("Done") ). when I close android program, it gets to last line and bmp opens succesfully
Your reading logic is completely off. You only use a readLine() once and then write that to file. The data that was written to the socket on the device side was binary. That means that trying to read it as if it were textual (as readLine() does) will return meaningless junk. The reason it's usually 401 bytes long is that readLine() will look for the first newline character combination and return everything up to that as a String. This is not what you want.
What you need is a loop that will read from the socket and write into the file as long as there is data in the socket. A standard copy loop should suffice here.
InputStream in = socket.getInputStream();
OutputStream out = new FileOutputStream(...);
final byte[] buffer = new byte[BUFFER_SIZE];
int read = -1;
while ((read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
in.close();
out.close();
Note that the above code isn't tested but something to that effect should do the trick.
Why are you reading a String if you are sending a byte ?
Try those setp one by one only if the previous did not worked.
1. Read() and don't Readline() what you are writing
If you write a byte, read a byte
Byte obj = SockIn.read();
2. Encode your array before sending
Base64.encodeBase64String(byteArray);
There are good resource over converting JPEG to base64.
I am particularly interested in doing it without decoding to bimap, avoiding any memory inflation. I also understand that any file can be encoded into Base64 by converting it into byte array first.
So if we can directly create a byte array of JPEG / PNG file which is far less than the byte array of a decoded jpeg bitmap we can convert it to base64 using less memory footprint.
The closest answer I have come across is this https://stackoverflow.com/a/10160856/499752
public void getGalleryDetails(String path) throws FileNotFoundException {
InputStream inputStream = new FileInputStream(path);
byte[] bytes;
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try{
while((bytesRead = inputStream.read(buffer)) != -1){
output.write(buffer, 0, bytesRead);
}
}catch(IOException e){
e.printStackTrace();
}
bytes = output.toByteArray();
encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
Log.i("ENCODED", encodedImage);
}
You could actually use this ... where you can provide the path of the file to be converted to Base64. Sorry for the late post ... Just say this post.