How to display image from blob data in android sqlite? - android

I have been trying to store image form android sdcard to the sqlite database. And it worked fine. The image is getting stored into the database as blob. This is the rough code I have been using for that.
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
imgView.setImageBitmap(bitmap);
int size = bitmap.getWidth() * bitmap.getHeight();
ByteArrayOutputStream out = new ByteArrayOutputStream(size);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
try {
out.flush();
out.close();
} catch (IOException e) {
e.printStackTrace();}
byte[] x = out.toByteArray();
In the database side, the code is like this
ContentValues values = new ContentValues();
byte[] bytes = null;
bytes = img_bytes.getBytes();
values.put("IMAGE", bytes);
And for displaying I have used the code as below.
ImageView image = new ImageView(this);
byte[] img_bytes = result.get("IMAGE").getBytes();
Bitmap bMap = BitmapFactory.decodeByteArray(img_bytes, 0, img_bytes.length);
image.setImageBitmap(bMap);
When I printed the string value of the byate array, it was getting like [B#44da2db0. But the byte array is not getting displayed into image.
Somebody please help me with this.
Thanks

I have found a solution. Thanks to all.
I have used base64 encoding method.
ie; For displaying the image, I have been using a hash map to get the result first of all from the database table.
So before saving into the map, I have encoded the bytes to string using base 64 method.
Then On the Activity side for displaying the image, I have decoded back the string using base 64 again and then converted to bytes and the image got displayed.

Related

How to add multiple bitmap images with text into a table using iText in android?

I want to create a pdf file containing barcode images with text in a table. I am not able to create in this format.
PdfPTable table = new PdfPTable(qrCodeModelArrayList.size());
for (int i = 0; i < qrCodeModelArrayList.size(); i++) {
Bitmap bitmap = qrCodeModelArrayList.get(i).getQrBitMap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); //use the compression format of your need
InputStream is = new ByteArrayInputStream(stream.toByteArray());
Bitmap bmp = BitmapFactory.decodeStream(is);
ByteArrayOutputStream stream1 = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream1);
Image bitmapImage = null;
try {
bitmapImage = Image.getInstance(stream1.toByteArray());
} catch (IOException e) {
e.printStackTrace();
}
PdfPCell cell = new PdfPCell();
cell.addElement(bitmapImage);
Paragraph p = new Paragraph(qrCodeModelArrayList.get(i).getQrCodeName());
p.setAlignment(Element.ALIGN_CENTER);
cell.addElement(p);
table.addCell(cell);
}
When using this code, I did got output as below:
I am not able to view my barcode images and all columns are adding in same row. I wanted it in 5 columns .
I have updated your question so that it shows the actual problem: you are trying to create a QRCode, which should be done using the BarcodeQRCode class. You should then add the code to the PDF using the placeBarcode() method. You can't use the createAwtImage() method, because there is no AWT on Android. I think that the code you are currently using has some image-related problem. A QRCode consists of vector data and you convert it to the lossy JPEG format (which is a bad idea, because barcode scanners will have trouble reading JPGs, you should use PNG instead). Also you are compressing the barcode; which doesn't make sense for an image that could be a vector format.
Using the placeBarcode() method will add the QRCode as a vector image (using PDF syntax). That is much better than adding a bar code as a raster image.

Why PNG forms black edges around the image for PNG format?

I select image from gallery, convert into Base64 and sends to server. For JPEG image, it works fine; the image I upload from gallery on server same gets shown in server folder. However, when I upload PNG format image from mobile gallery, it doesn't show same on server; instead it creates black edges around it. I really don't know what's going wrong?
Also, my actual image is as equal as given JPEG image.
Reference images:
JPEG:
PNG:
I just want to get rid of BLACK borders which should not appear for PNG format images.
Below is my code snippet
FileInputStream mFileInputStream = null;
try {
mFileInputStream = new FileInputStream(imagePathFromSDCard);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
Bitmap bitmap = safeImageProcessing.decodeFile(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] ba = bos.toByteArray();
String encodedImage = Base64.encodeToString(ba, Base64.NO_WRAP);
//this line sends image base64 to server & there i decode into original
new ImageAsync().sendImageProcess(getActivity(), encodedImage, this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You want to upload image files. But you use BitmapFactory to make a Bitmap out of them first. Then you compress the bitmap to a jpg or png byte array. After that you base64 encode the bytes to a string which you upload.
What a waiste. And you have changed the files. Do away with the intermediate bitmap.
Instead load the files directly in a byte array. Then continue with the byte array as usual.
Having said that i think its a bad idea to base64 encode the bytes of a file first as it increases the amount of bytes that have to be transferred with 30%.

Why is my BitmapFactory.decodeByteArray returning null?

I am trying to convert an image stored in Database in Base64 format, to a Bitmap to be used in an Imageview.
So, I store it in SQLite this way:
Bitmap imageBitmap = (Bitmap) extras.get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap fotoGrande=(Bitmap) extras.get("data");
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
//I am adding some data to EXIF here, add to an static ArrayList<Bitmap> in other class and I store it this way:
int bytes=listaFotos.get(i).getFoto().getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes);
listaFotos.get(i).getFoto().copyPixelsToBuffer(buffer);
values.put("foto", Base64.encodeToString(buffer.array(), Base64.DEFAULT));
Later, i need to get that image to fit it in an ImageView:
String foto = csr2.getString(0);//csr2 is a cursor
byte[] arrayFoto = Base64.decode(foto, Base64.DEFAULT);//This is not null
Bitmap fotoBitmap = BitmapFactory.decodeByteArray(arrayFoto, 0, arrayFoto.length);//This is null
I know there are tons of questions about this. I searched, but no answer fix my problem.
Why is my BitmapFactory.decodeByteArray returning null? What I am doing wrong? Any help?
Thank you.
Turned out to be a database issue. SQLite gives you a cursor of 1MB MAX size. I was getting the bytes from database, with a 1MB cursor, the pic was not sent properly.
To fix it, I stored the path to the photo in database instead of the bytes.
firstly image is convert in Bitmap to String like this
ByteArrayOutputStream stream = new ByteArrayOutputStream();
camera.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte imageInByte[] = stream.toByteArray();
String encodedImage = Base64.encodeToString(imageInByte, Base64.DEFAULT);
where camera is a Bitmap.
and store the String encodedImage in database
And getImage string like this
byte[] b = Base64.decode(encodedImage , Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);

Insert Image to mysql database in android using PHP

I have a problem to save a image to mysql using PHP.
I have some code but it's got some errors.
Here is my code.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.id.img_upload);
//img_Photo.setImageBitmap(bitmap);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
image_str= Base64.encodeToString(byte_arr, 1);
postParameters.add(new BasicNameValuePair("photo", image_str));
Here, bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); is always got Null.
My code is wrong or which part should i repair. Give me some advices.
Change your line as below:
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.img_upload);
If you want to get the image from ImageButton try out as below:
BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
Bitmap bitmap = drawable.getBitmap();
first convert your image into a base 64 byte array encoded string. after that send it to php. extract on server side.Then store that string in MySQL. after that send that string to android client. extract image string and decode with base 64 decode. after that you will get byte array you can simply show in your image view. for your reference I will show some code
String imagedata = Base64.encodeToString(thumbnailArray,Base64.DEFAULT);
mJobject.put("imagebyte",imagedata);
mJArray.put(mJobject);
JSONArray json=new JSONArray(response);
JSONObject jo = null;
imageArray=new String[json.length()];
imageArray[i]=jo.getString("imageid");
completeImage= Base64.decode(imageArray[0],Base64.DEFAULT);
Bitmap bitmap = BitmapFactory.decodeByteArray(completeImage , 0, completeImage.length);

Image Converted to Byte not shown when Converting back to Image

In my android application , i want to convert image into bytes and encode it. and send to database. But when i convert it back on image, it do not show. Please help.and tell me where i am making mistake
final Bitmap image=(images.get(position));
int size = image.getRowBytes() * image.getHeight();
ByteBuffer buffer = ByteBuffer.allocate(size); //Create a new buffer
image.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] array = buffer.array();
encodedImageString = Base64.encodeToString(array, Base64.DEFAULT);
Now on the server side when i decode this encoded imageString and write it, it do not display image.
Byte[] imageByteArary= base64.decode(encodedImageString);
File myfile=new File("D://test1.jpg");
myfile.createNewFile();
FileOutputStream fOut=new FileOutputStream (myfile);
fOut.write(imageByteArray);
fOut.close();
I was facing the same problem.The String that you send from client side is not same what you receive at server side.
Check this for solution

Categories

Resources