1

I'm saving the images to mysql database using PHP. The images reach to the save file and database but they are in invalid format.Can't open. How to store image in correct method. Show me some ways. Here is my code.

 Bitmap bm = ShrinkBitmap(picturePath, 300, 300);
            resizedbitmap = Bitmap.createScaledBitmap(bm, 100, 100, true);

            ByteArrayOutputStream baos=new  ByteArrayOutputStream();
            resizedbitmap.compress(Bitmap.CompressFormat.PNG,100, baos);
            byte [] arr=baos.toByteArray();
            imageSave = Base64.encodeToString(arr, Base64.DEFAULT);

            img_Photo.setImageBitmap(resizedbitmap);



 Bitmap ShrinkBitmap(String file, int width, int height){

     BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
        bmpFactoryOptions.inJustDecodeBounds = true;
        Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

        int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
        int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

        if (heightRatio > 1 || widthRatio > 1)
        {
         if (heightRatio > widthRatio)
         {
          bmpFactoryOptions.inSampleSize = heightRatio;
         } else {
          bmpFactoryOptions.inSampleSize = widthRatio;
         }
        }

        bmpFactoryOptions.inJustDecodeBounds = false;
        bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
     return bitmap;
    }



 postParameters.add(new BasicNameValuePair("photo", imageSave));

Edit: Here is PHP.

 if(isset($_POST["photo"]) && !is_null($_POST["photo"]))
                    {
                        $data = mysql_real_escape_string($_POST["photo"]);   
                        //create folder
                        if(!file_exists('User_Photo/'))
                        {
                            mkdir('User_Photo/', 0777, true);
                        }
                        //upload image
                        define('UPLOAD_DIR', 'User_Photo/');
                        $img = $data;
                        $img = str_replace('data:image/png;base64,', '', $img);
                        $img = str_replace(' ', '+', $img);
                        $data = base64_decode($img);
                        $file = UPLOAD_DIR . $str . '.png';
                        $query .= "       , v_photo";
                        $value .= "       , '".$file."'";
                    }
                    else
                    {
                        $flag = true;
                    }
Halo
  • 729
  • 1
  • 8
  • 18
  • You save the images as base64 encoded strings. How do you know they are not valid, could you show us the decoding code? – molnarm Jan 17 '14 at 11:17
  • I already posted decode file. ShrinkBitmap(picturePath, 300, 300); – Halo Jan 17 '14 at 11:19
  • If I understand your code correctly, you open an image in your Android app, call `ShrinkBitmap`, then post it to the server. How do you know they are in invalid format? I guess you display them on some webpage from PHP, that is the code I asked for. – molnarm Jan 17 '14 at 11:24
  • The image reached to the saved file but when I open the image, it show black image only. I didn't saved black image but I don't know why they changed black image. – Halo Jan 17 '14 at 11:31
  • postParameters.add(new BasicNameValuePair("photo", imageSave)); This is the part to save PHP. – Halo Jan 17 '14 at 11:32
  • You just write the string received in POST to a file? It is base64 encoded, so of course it is invalid... I asked for the PHP code. – molnarm Jan 17 '14 at 11:32
  • You want to say imageSave = Base64.encodeToString(arr, Base64.DEFAULT); is something wrong. So, what should I do? – Halo Jan 17 '14 at 11:40
  • Posting an image as string makes no sense: base64 actually increases its size, and your string escaping/replacements can corrupt it. Just [post the file itself](http://stackoverflow.com/questions/13002547/android-httppost-with-parameters-and-file). – molnarm Jan 17 '14 at 11:47
  • Can I save Image path only? – Halo Jan 17 '14 at 12:22

0 Answers0