我正在使用PHP将图像保存到mysql数据库。图像到达保存文件和数据库,但它们的格式无效。无法打开。如何以正确的方法存储图像。告诉我一些方法。这是我的代码。
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));
编辑:这是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;
}