0

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.

4
  • Imagebutton from xml. Commented Dec 31, 2013 at 5:51
  • try Base64.encodeBytes(byte_arr); Commented Dec 31, 2013 at 6:03
  • can't change Base64.encodeBytes(byte_arr); In my app, Base64 doesn't have encodeBytes(); Commented Dec 31, 2013 at 6:07
  • @user3032822 Check out my answer and try to change accordingly. Commented Dec 31, 2013 at 6:13

2 Answers 2

2

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();
Sign up to request clarification or add additional context in comments.

12 Comments

img_upload is Imagebutton. :)
You can not get the image from the image button in this way. Are you trying to get image from ImageButton ?
Yes it should be from drawable only not from views.
Yes, I must store an image from gallery to mysql database. In that case, I also show an image that is from gallery onto the imagebutton.
Check out this tutorial androidhub4you.com/2012/09/… it will help you. @user3032822
|
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.