从Xamarin C上传图像从android到php服务器#
问题描述:
我试图从Android应用上传图像到Php服务器。 我找到了上传图片的方法,用c#编码为base64,并使用php base64解码器再次解码。 一切运作良好,我可以成功地将图片上传到服务器,但我上传后,图像剂量不会打开使用任何图像浏览器我得到这个错误从Xamarin C上传图像从android到php服务器#
无法打开此文件
它只是在铬或边缘Internet Explorer或任何其他Internet Explorer中工作。 我希望你能帮助我使用像Windows图片查看器图像浏览器 我的C#代码是这样打开它:
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";
}
}
答
不要使用Bitmap.CompressFormat.Webp
,但Bitmap.CompressFormat.PNG
(或jpeg如果有损照片压缩是好的)。
WebP是一种相对较新的图像格式,目前尚未得到广泛支持。