2017-10-15 176 views
0

我試圖從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"; 
    } 


} 

回答

1

不要使用Bitmap.CompressFormat.Webp,但Bitmap.CompressFormat.PNG(或jpeg如果有損照片壓縮是好的)。

WebP是一種相對較新的圖像格式,目前尚未得到廣泛支持。

相關問題