简体   繁体   English

将图像从Android上传到Xamarin C#中的php服务器

[英]Upload image from android to php server in Xamarin C#

Im trying to upload images from Android app to Php Server . 我试图将图像从Android应用程序上传到Php服务器。 I found way to upload the image by encode it as base64 using c# and decode it again using php base64 decoder . 我找到了通过使用c#将其编码为base64并使用php base64解码器再次对其进行解码的方法来上传图像。 Everything working well i can upload the image to the server successfully but after i upload it, the images dose not open using any image viewer i got this error 一切正常,我可以将图像成功上传到服务器,但是在上传之后,使用任何图像查看器都无法打开图像,但出现此错误

couldn't open this file 无法打开此文件

it just works inside the chrome or edge internet explorer or any other internet explorer. 它仅在chrome或Edge Internet Explorer或任何其他Internet Explorer中运行。 I hope you could help me to open it using image viewer like windows images viewer my c# code is like : 我希望你能帮助我使用像Windows images Viewer这样的Image Viewer打开它,我的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;
    }
} 

And my php code : 和我的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";
    }


}

Don't use Bitmap.CompressFormat.Webp , but Bitmap.CompressFormat.PNG (or jpeg if lossy photo compression is okay). 不要使用Bitmap.CompressFormat.Webp ,而Bitmap.CompressFormat.PNG使用Bitmap.CompressFormat.Webp (如果可以进行有损照片压缩,则使用jpeg)。

WebP is a relatively new image format that is not widely supported yet. WebP是一种相对较新的图像格式,目前尚未得到广泛支持。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM