store and view image blob html.fromhtml android - android

I am trying to show a image saved as blob in my DB. I am using ORMLite and Android 1.6.
I created a textView who show a 'html.FromHtml'
Here is the code:
//Save the image in DB.
...
Bitmap myBitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString()+"/images/imagem.png");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
glossary.setImageBytes(byteArray);
'glossary' is an instance of Glossary. Who has the field 'imageBytes':
#DatabaseField(dataType = DataType.BYTE_ARRAY)
private byte[] imageBytes;
In my activity I pass this 'imageBytes' to string to use 'html.FromHtml':
//Activity
...
String htmlContent = "<h1> Title </h1>
<img src=\""+glossary.getImageBytes.toString()+"\" />"
Now in my Adapter I try to show the content of 'htmlContent':
//Adapter
...
holder.tvContent.setText(Html.fromHtml(htmlContent, new Html.ImageGetter() {
#Override
public Drawable getDrawable(String source) {
byte[] data;
data = source.getBytes();
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
//Log.i("bitmap", "bitmap height:" + String.valueOf(bitmap.getHeight) );
//THIS LOG RETURN NPE
Drawable d = null;
d = new BitmapDrawable(getResources(),bitmap);
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
}, null));
...
I think the problem is when I change byte to string and string to byte. Because the bitmap created by 'BitmapFactory.decodeFile()' is returning a NPE.
But I don't know how fix that.
Any suggestions?
thx!

Well certainly your first problem is that you should not be doing:
glossary.getImageBytes().toString()
To turn a byte[] into a String you should do:
new String(glossary.getImageBytes())
If you do a byte[].toString() it will be set to something like:
[B#3487a5cc
Instead of a String made up of the bytes in the array.
That said, I'm not sure you can just put the bytes from a PNG file into a byte[] array to be printing out as a String. That sounds like you are going to be printing binary information in your HTML which I don't think will convert correctly. Also, it looks like you are putting the PNG bytes into the src="..." HTML section. That won't work either AFAIK.

Related

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);

ImageView data to Byte Array Android

I am trying to use parse.com database to upload file and syntax needs me to use byte []. I will write syntax below:
byte[] data = "Working at Parse is great!".getBytes();
ParseFile file = new ParseFile("resume.png", data);
file.saveInBackground();
ParseObject jobApplication = new ParseObject("JobApplication");
jobApplication.put("applicantResumeFile", file);
jobApplication.saveInBackground();
I need to know how to use the first instruction to get ImageView Data in byte[] and upload it.
#wisejoy I have an implementation but I think its a little bit different... hope it can help you... cheers!!
myBitmap = (Bitmap) data.getExtras().get("data");//here I set an image taken by the camera
imageView.setImageBitmap(myBitmap);//I set it into an image view
imageView.buildDrawingCache();
ByteArrayOutputStream baos = new ByteArrayOutputStream();//and then I convert that image into a byteArray
myBitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
encondedImage = android.util.Base64.encodeToString(b, Base64.DEFAULT);
Log.e(TAG, "" + txt);

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);

Android - converting File or Byte array into a Blob

I've been searching a lot but i couldn't find an answer for this simple question.
I would like to implement one of the following functions:
public Blob getBlob(Byte[] imageByteArray){
}
public Blob getBlob(File imageFile){
}
please note that these functions are being called from the android client.
thanks!
//you bitmap image first get
Bitmap bitmap = BitmapFactory.decodeFile("/path/images/image.jpg");
//take on bytearrayoutputStream to convert into blolb so here is you blob
ByteArrayOutputStream blob = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 "ignore png", blob);
byte[] bitmapdata = blob.toByteArray();

How to display image from blob data in android sqlite?

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.

Categories

Resources