Insert Image to mysql database in android using PHP - android

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

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

Image Doesn't Show in my Android ImageView

I am creating android web application using RESTFul web services.i have declared code to save Image like this
onCreateMethod()
ImageView iv = (ImageView) findViewById(R.id.hotelLogoImageView);
iv.setDrawingCacheEnabled(true);
onSave()
Bitmap b = ((BitmapDrawable) iv.getDrawable()).getBitmap();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, bos);
logo = bos.toByteArray();
then i am using RESTFul webservices.so i converted logo(byte Array) to String and passed logo into NameValuePair like:
private ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("hotel_logo",String.valueOf(logo)));
In my server side code, again i converted my String value to byteArray like:
byte[] imgLogo = hotelParams.getFirst("hotel_logo").getBytes(
Charset.forName("UTF-8"));
Finally i saved imgLogo value in my phpMyAdmin Database.it's also Stored in Database like [BLOB - 10 B]
Now I have to set that image into ImageView.so I have used this method.like
JSONObject jsonObject = new JSONObject(response);
byte[] logo = Base64.decode(jsonObject.getString("hotel_logo"),
Base64.DEFAULT);
Bitmap hotel_Logo = BitmapFactory.decodeByteArray(logo, 0, logo.length);
hotelLogo.setImageBitmap(hotel_Logo);
After finish this code.there is no error.But Image Doesn't Show in ImageView.please Help me what i have done mistake?

Decoded String Image Can't Be Display As BitMap in Android

Below is the portion of code I used to encode the image file from bitmap to base64 string by import android.util.Base64 in my android project:
`
Bitmap img_bmp=BitmapFactory.decodeStream(getContentResolver().
openInputStream(this.browseImageURI));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img_bmp.compress(Bitmap.CompressFormat.JPEG, 30, baos);
byte[] image = baos.toByteArray();
String profile_img = Base64.encodeToString(image, Base64.DEFAULT);
`
The string profile_img will be saved in mysql database as a string.
When I retrieve the string value from database, I will decode image string from String to Bitmap by using the below code:
`
Intent i = getIntent(); //pass the value from previous activity
str_img= i.getStringExtra("img");
img_bm = StringToBitMap(str_img);
imgview = (ImageView)findViewById(R.id.imageView1);
imgview.setImageBitmap(img_bm); // display the image
//Function to convert string to bitmap
public Bitmap StringToBitMap(String image){
try{
byte [] encodeByte=Base64.decode(image,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
return bitmap;
}catch(Exception e){
e.getMessage();
return null;
}
}
`
I expected it will display the image, however the image is not displayed and I get a log message from the Base64 decoder "--- decoder->decode returned false".
Can someone help me to figure out what is wrong in my code?
And do anyone know how to convert the base64 string image (pass from JSON to php script) into a blob format so that I can store it as BLOB in mysql.
Thank you in advance.

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