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