dongyong3223 2017-10-15 11:38
浏览 84
已采纳

在Xamarin C中将图像从android上传到php服务器#

Im trying to upload images from Android app to Php Server . I found way to upload the image by encode it as base64 using c# and decode it again using php base64 decoder . 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. I hope you could help me to open it using image viewer like windows images viewer my c# code is like :

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 :

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";
    }


}
  • 写回答

1条回答 默认 最新

  • duanhanglekr37902 2017-10-15 11:42
    关注

    Don't use Bitmap.CompressFormat.Webp, but Bitmap.CompressFormat.PNG (or jpeg if lossy photo compression is okay).

    WebP is a relatively new image format that is not widely supported yet.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?