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 :)
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.
Is there a way to know if a byte array is a valid image?
I am taking a photograph, In OnActivityResult:
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 75, out);
//arrayFotos is an ArrayList
arrayFotos.add(new BeanFotos("", imageBitmap, lastKnwonLocation.getLatitude(), lastKnwonLocation.getLongitude()));
I am storing it in database:
//listaFotos and arrayFotos are the same array, but with different names, because I am storing it in another class in my app
for(int i=0;i<listaFotos.size();i++){
values.clear();
values.put("codficha", codficha);
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", buffer.array());
db.insert("fotos", null, values);
}
Later, I need that image to be shown:
byte[] arrayFoto = csr2.getBlob(0);
Bitmap fotoBitmap=BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);
The problem is, no matter what i do, the decodeByteArray method always returns null. I have tried with external libraries, and it is always null, so I am thinking maybe I am storing it in the wrong way. The arrayFoto variable is NOT null, I am receiving bytes (84584, to be exact) but the decodeByteArray method returns null.
So, is there a way for me to be sure that the byte array is a correct image?
Thanks.
It seems you are writing the uncompressed Bitmap to the database. You need to compress it to jpeg or png first if you want to be able to decode it using BitmapFactory.
Also, storing images in a database is a bad idea since the cursor window size is limited to 1MB. You should not store binary data in a SQLite db unless it's very small.
Recently I was working on MySql and Sql, where I found I can almost insert everything primitive type data but is it possible to store a jpeg/jpg/bmp/png (image)file in a database. If so then can you please write the code. Don't need to explain the whole thing just the key point.
Thanks
1 Perhaps convert the image to a Byte Array (if it's not bmp, then convert it), then store it in MySql as a Blob type, then when reading it, you can create a Bitmap through the code (I don't know what language you're using) with the Byte Array with what you read, or perhaps just get the code associated with the image and store that and read it.
Converting an image to a Byte Array
2 Use an image hosting API (like Imgur) and have the user's image upload to that site, and just read the URI from the database whenever you want to use it.
Imgur API, Android Example
there is 3 cases how to make it (that is i know)
Save path from SD card like String in database
Save int value if image is in res folder of your app.
Save url like String if image is in Internet.
Sorry for the late response. However, I found a way to convert the image to String and then store it in the mysql.
This answer is just for those who are still looking for a easy solution:
code:
Bitmap bm = BitmapFactory.decodeFile("/path/to/myImage.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
and then:
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
Then the encodedImage which is a String format, I can insert into the db.
Hope that helps other who are still looking for the answer.
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...)
I want to send the bitmap image through the bluetooth along with some other contents like char and int. The problem is to convert those things into single byte array. I tried by making it as two byte array and merging them but copyTo is not working. Is there some other way to do it?
Use System.arraycopy method to copy one array into another
int lenA = arrayA.length;
int lenB = arrayB.length;
byte[] outArray = new byte[lenA + lenB];
System.arraycopy (arrayA, 0, outArray, 0, lenA);
System.arraycopy (arrayB, 0, outArray, lenA, lenB);
I haven't tested it, but should work.
edit:
And of course it's not recommended for big arrays. You're doubling data in memory in that way. I don't know what you're doing exactly with this data but if you can, use streaming instead.