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.

insomniac
  • 11,146
  • 6
  • 44
  • 55
user3032822
  • 113
  • 2
  • 6
  • 18

2 Answers2

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();
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
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);
insomniac
  • 11,146
  • 6
  • 44
  • 55
skyshine
  • 2,767
  • 7
  • 44
  • 84