Android:
I have two byte arrays one byte array of bitmap and another byte array of String.
I have combine these two byte arrays into 3rd one.
I will write 3rd byte array into storage.
When i read that image which is created from combine byte array of image and string i want to know in byte[] that where image bytes ends and where string bytes gets start.
How do i know the byte representation of end of image byte ? or starting of string byte ?
Thanks in advance.
Before you write out the byte array, you should write out the length of the image part. That way you'll know how much data to read. So your file will look like this:
Length of image (4 bytes, probably - use DataOutputStream for simplicity)
Image data
Text data
(As an aside, I suggest you explicitly write out the text data as UTF-8, and read it as UTF-8. Or you could just use DataOutputStream.writeUTF and DataInputStream.readUTF of course...)
Related
I need to convert an audio Uri to a byte array in order to store it in a SQLite BLOB. I already managed to do that with images, by converting them into bitmaps first, but I have no idea on how to do this for audios as well.
Could you please help me?
do that with images, by converting them into bitmaps first,
Well that is a bad approach as that bitmap takes an awfull lot of memory. And after that you compress it to a baos (ByteArrayOutputStream) i think. And then convert to byte array. So the complete file is in the baos and in the array. All spilled memory.
Instead you should directly declare a byte array with the size of the file and then load the bytes of the file in that array. Thats all.
Once you have done that for images your code is ready for all files. No difference. Just loading the bytes of a file in a byte array.
I am working on the android library for encoding/decoding raw data through ffmpeg. Every example I found uses files, it either reads or writes to a file. However, I am using raw byte array representing RGBA image for encoder input and byte array for encoder output. Lets focus on encoding part for this question.
My function looks like this:
int encodeRGBA(uint8_t *image, int imageSize, int presentationTimestamp,
uint8_t *result, int resultSize)
Where image is byte array containing raw rgba image data, imageSize is length of that array, presentationTimestamp is just counter used by AVFrame for setting pts, result is preallocated byte array with some defined length (currently with size matching width x height) and resultSize is byte array length (width x height). Returned int value represents actually used length of preallocated array. I am aware that this is not the best approach for sending data back to java and this is also part of the question. Is there a better way for returning result?
Example found here for encoding, directly writes byte data to the frame->data[0] (different approach for different formats, RGBA or YUV). But google search for "ffmpeg read from memory" results in examples like this, this or this. All of them suggesting using AVIOContext.
I am confused how to use AVFormatContext with AVCodecContext for encoding?
Currently I have encoder working using first approach and I am successfully returning results as described (with preallocated byte array). I would like to know if that is wrong approach? Should I be using AVIOContext for handling byte arrays?
Should I be using AVIOContext for handling byte arrays?
No. AVIOContext if for working with files and or containers in memory. In that case avformat is required to read encoded frames out of A byte array. You are working with raw frames directly and don’t require using avformat.
I want to calculate json array Size in KB/Mb. I have a condition where I need to calculate json Array size before uploading. It should not be more than 128 KB.
Convert your jsonArray to String and use string.getBytes().length . It will give number of bytes used by the string to store the value.
Using those bytes you can calculate the size in any unit.
String.getBytes().length is the number of bytes needed to represent
your string in the platform's default encoding. For example, if the
default encoding was UTF-16 (rare), it would be exactly 2x the value
returned by String.length(). More commonly, your platform encoding
will be a multi-byte encoding like UTF-8.
JSON is basically an String and encodings determine how much memory is required to store a String. Please read this first.
Now with that knowledge you can simply proceed as follow:
JSON.toString().getBytes(YOUR_PREFERRED_ENCODING).length;
you can use the File.length() method to get the file size in bytes.
i have one questions, how to convert Multiple picture to byte array (byte []), cause i have case to save many picture in my database sqlite, i have array list which contains picture in drawable folder..
ArrayList<Integer> imageId = new ArrayList<Integer>();
imageId.add(R.drawable.a1);
imageId.add(R.drawable.a2);
imageId.add(R.drawable.a3);
imageId.add(R.drawable.a4);
imageId.add(R.drawable.a5);
Then i have tried this code to convert into byte arrray
Bitmap b = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.a1);
//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
System.out.println(array);
The code below is working, but the problem is how the code below is convert one picture, and now i need to convert multiple picture in arrayList, can anybody help me? cause i have tried with looping, it give me an error, java.lang.OutOfMemoryError..
Ok,
first of all do not save whole pictures to SqLite. It is not a suitable way. There's a very easy solution but a little risky..
Just save images to SD Card as WhatsApp do and save their path to SqLite. So whenever you want to access your pictures you can read their paths from Sqlite and access them.
But the risk is pictures are in user control, so they can remove it. But you can hide the pictures in a deep way :)
Is there a mechanism in Android, which allows us to convert a large image into a Hex String Array before we send it to a webservice over the network?
I know how to convert it to a byte array and transfer it but I'm not so confident on how to implement the same using a Hex String.
Thanks in advance.
No direct method to do this. Convert to byte[] first. Then, convert to hex string like in answers to question "In Java, how do I convert a byte array to a string of hex digits while keeping leading zeros?"