C# 将图像从android上传到Xamarin C中的php服务器#
我正在尝试将图像从Android应用程序上传到Php服务器。 我找到了上传图像的方法,使用c#将其编码为base64,然后使用php base64解码器再次解码。 一切正常,我可以上传图像到服务器成功,但在我上传后,图像没有打开使用任何图像查看器我得到了这个错误 无法打开此文件 它只在chrome或edge internet explorer或任何其他internet explorer中工作。 我希望您能帮助我使用图像查看器(如windows图像查看器)打开它 我的c#代码如下:C# 将图像从android上传到Xamarin C中的php服务器#,c#,php,android,image,xamarin,C#,Php,Android,Image,Xamarin,我正在尝试将图像从Android应用程序上传到Php服务器。 我找到了上传图像的方法,使用c#将其编码为base64,然后使用php base64解码器再次解码。 一切正常,我可以上传图像到服务器成功,但在我上传后,图像没有打开使用任何图像查看器我得到了这个错误 无法打开此文件 它只在chrome或edge internet explorer或任何其他internet explorer中工作。 我希望您能帮助我使用图像查看器(如windows图像查看器)打开它 我的c#代码如下: public
public override void OnActivityResult(int requestCode, Result resultCode, Intent data)
{
if ((requestCode == PickImageId) && (resultCode == Result.Ok) && (data != null))
{
Stream stream = Activity.ContentResolver.OpenInputStream(data.Data);
Bitmap bitmap = BitmapFactory.DecodeStream(stream);
MemoryStream memStream = new MemoryStream();
bitmap.Compress(Bitmap.CompressFormat.Webp, 100, memStream);
byte[] picData = memStream.ToArray();
WebClient client = new WebClient();
Uri uri = new Uri(statics_class.request_url+"request.php");
NameValueCollection parameters = new NameValueCollection();
parameters.Add("Image", Convert.ToBase64String(picData));
parameters.Add("Image_id", image_id.ToString());
parameters.Add("user_id", user_id);
client.UploadValuesAsync(uri, parameters);
client.UploadValuesCompleted += Client_UploadValuesCompleted;
}
}
和我的php代码:
if (isset($_POST['Image']) && isset($_POST['Image_id']) && isset($_POST['user_id']))
{
$user_id = $_POST['user_id'] ;
$Image_id = $_POST['Image_id'] ;
$now = DateTime::createFromFormat('U.u',microtime(true));
$id = $now->format('YmdHisu');
$upload_folder = "upload/$user_id";
if(!is_dir($upload_folder)){
mkdir($upload_folder);
}
$path = "upload/$user_id/$id.jpg";
$image = $_POST['Image'] ;
if(file_put_contents($path , base64_decode($image)) != false){
printf('<img src="data:image/jpg;base64,%s" />', $image);
echo "successed ";
exit;
}else{
echo "failed";
}
}
if(isset($\u POST['Image'])和&isset($\u POST['Image\u id'])和&isset($\u POST['user\u id']))
{
$user\u id=$\u POST['user\u id'];
$Image\u id=$\u POST['Image\u id'];
$now=DateTime::createFromFormat('U.U',microtime(true));
$id=$now->format('YmdHisu');
$upload\u folder=“upload/$user\u id”;
如果(!is_dir($upload_folder)){
mkdir($upload_folder);
}
$path=“upload/$user\u id/$id.jpg”;
$image=$_POST['image'];
if(file\u put\u contents($path,base64\u decode($image))!=false){
printf(“”,$image);
呼应“成功”;
出口
}否则{
echo“失败”;
}
}
不要使用Bitmap.CompressFormat.Webp
,而是使用Bitmap.CompressFormat.PNG
(如果有损照片压缩可以,则使用jpeg)
WebP是一种相对较新的图像格式,目前尚未得到广泛支持