嗨,我正在尝试从我的应用程序上传图像,并希望将其保存在我的web文件夹中。我已经使用了下面提到的php代码,但文件正在保存为txt,但我需要jpeg文件以备将来使用。
PHP代码-
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['name'];
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under 'www/domain.com/images' folder
$file = fopen('images/'.$filename, 'wb');
// Create File
fwrite($file, $binary,);
fclose($file);
echo 'Image upload complete, Please check your php file directory';
?>
当您从应用发布图像时,请尝试此代码
try {
Uri myUri = Uri.parse(image1);
Bitmap bmp = BitmapFactory.decodeStream(getContentResolver()
.openInputStream(myUri));
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
image = Base64.encodeToString(imageBytes, Base64.DEFAULT);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
List<NameValuePair> params1 = new ArrayList<NameValuePair>();
params1.add(new BasicNameValuePair("image", image));
这里,下面的代码帮助您使用PHP将图像存储在服务器上。我假设您在post参数中得到图像名称。
$binary = base64_decode($image);
$filename = $_REQUEST['name'];
file_put_contents('images/'.$filename, $binary);
我自己做了这件事,但感谢你们的支持。
<?php
// Get image string posted from Android App
$base=$_REQUEST['image'];
// Get file name posted from Android App
$filename = $_REQUEST['name'].'.jpg';
// Decode Image
$binary=base64_decode($base);
header('Content-Type: bitmap; charset=utf-8');
// Images will be saved under 'www/domain.com/images' folder
$file = fopen('images/'.$filename, 'wb');
// Create File
fwrite($file, $binary,);
fclose($file);
echo 'Image upload complete, Please check your php file directory';
?>