2017-02-27 52 views
0

目前正在使用Volley上傳一個Image到服務器,但圖片上傳0kb,沒有名稱甚至,我從Android上傳圖片的方式,我首先把我的位圖到一個String然後,在服務器端的C#代碼轉字符串回Bitmap,下面是我的Java代碼:圖片上傳android Java + Asp.net(C#)

private String UPLOAD_URL ="http://xxxxx:8092/PoliceApp/ImageUpload.aspx"; 

    private void onUploading() { 

      final ProgressDialog loading = ProgressDialog.show(this,"Uploading...","Please wait...",false,false); 
      StringRequest stringRequest = new StringRequest(Request.Method.POST, UPLOAD_URL, 
        new Response.Listener<String>() { 
         @Override 
         public void onResponse(String s) { 
          //Disimissing the progress dialog 
          loading.dismiss(); 
          //Showing toast message of the response 
          Toast.makeText(CrimesReporting.this, s , Toast.LENGTH_LONG).show(); 
         } 
        }, 
        new Response.ErrorListener() { 
         @Override 
         public void onErrorResponse(VolleyError volleyError) { 
          //Dismissing the progress dialog 
          loading.dismiss(); 
          //Showing toast 
          Toast.makeText(CrimesReporting.this, volleyError.getMessage().toString(), Toast.LENGTH_LONG).show(); 
         } 
        }){ 
       @Override 
       protected Map<String, String> getParams() throws AuthFailureError { 
        //Converting Bitmap to String 
        selectedFilePath = getStringImage(bitmap); 
        // Uri selectedImageUri = data.getData(); 
        //String video = getVideo(selectedImageUri); 
        File fileAttachment; 
        //Getting Image Name 
        String contact = contact_crimes.getText().toString().trim(); 
        String PersonalContact = information_crimes_edt.getText().toString().trim(); 
        String CrimesList = spinner.getSelectedItem().toString(); 
        //Creating parameters 
        Map<String,String> params = new Hashtable<String, String>(); 
        //Adding parameters 
        params.put("CrimeContact", contact); 
        params.put("CrimeInformation", PersonalContact); 
        params.put("CrimeDate", CrimesList); 
        params.put("photo",selectedFilePath); 

        //returning parameters 
        return params; 
       } 
      }; 
      //Creating a Request Queue 
      RequestQueue requestQueue = Volley.newRequestQueue(this); 
      //Adding request to the queue 
      requestQueue.add(stringRequest); 
     } 

而且這是在服務器端的代碼,以將圖像上傳至服務器(使用Asp.net和C#)。但我有沒有把圖像和它在這個方法

SaveImage(ImagePic,ImageName); 

下面的名字是代碼:

public partial class PoliceApp_ImageUploadaspx : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 
     string ImagePic= ""; 
     string ImageName= ""; 
     SaveImage(ImagePic,ImageName); 
    } 
    public bool SaveImage(string ImgStr, string ImgName) 
    { 
     String path = HttpContext.Current.Server.MapPath("~/ImageStorage"); //Path 
     //Check if directory exist 
     if (!System.IO.Directory.Exists(path)) 
     { 
      System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist 
     } 
     string imageName = ImgName + ".jpg"; 
     //set the image path 
     string imgPath = Path.Combine(path, imageName); 
     byte[] imageBytes = Convert.FromBase64String(ImgStr); 
     File.WriteAllBytes(imgPath, imageBytes); 
     return true; 
    } 
} 

回答

0

你只是在發送您的要求的圖像的路徑。此路徑將無法從您的服務器訪問。我也不建議使用StringRequest發送圖像。相反,我會用這樣的:

public class ImagePostRequest<T> extends Request<T> { 
    private final byte[] body; 

    public ImagePostRequest(Bitmap bitmap) { 
     super(Request.Method.POST, UPLOAD_URL, new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError volleyError) { 
      } 
     }); 
     this.body = getBytesFromBitmap(bitmap); 
    } 

    // convert from bitmap to byte array 
    public static byte[] getBytesFromBitmap(Bitmap bitmap) { 
     ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
     bitmap.compress(Bitmap.CompressFormat.JPEG, 70, stream); 
     return stream.toByteArray(); 
    } 

    @Override 
    public String getBodyContentType() { 
     return "jpg/jpeg"; 
    } 

    @Override 
    public byte[] getBody() { 
     return this.body; 
    } 
} 
0

最後我得到了自己的答案,在C#代碼,我不得不要求,且均在的Android的JavaVolley Library

params.put("ImagePic",selectedFilePath); 
params.put("ImageName",timeStamp); 
參數

C#代碼請求android參數如上:

 string ImagePic = Request["ImagePic"]; 
     string ImageName = Request["ImageName"]; 

由於數據來自android被參數化,所以您必須請求它。

注:

Android的Java代碼的工作很好,我只是修改了C#代碼請求對圖像

下面是在保存圖片到服務器的完整的C#代碼。

protected void Page_Load(object sender, EventArgs e) 
    { 

     string ImagePic = Request.QueryString["ImagePic"]; 
     string ImageName = Request.QueryString["ImageName"]; 

     SaveImage(ImagePic,ImageName); 
    } 


    public bool SaveImage(string ImgStr, string ImgName) 
    { 
     String path = HttpContext.Current.Server.MapPath("~/ImageStorage"); //Path 

     //Check if directory exist 
     if (!System.IO.Directory.Exists(path)) 
     { 
      System.IO.Directory.CreateDirectory(path); //Create directory if it doesn't exist 
     } 

     string imageName = ImgName + ".jpg"; 

     //set the image path 
     string imgPath = Path.Combine(path, imageName); 

     byte[] imageBytes = Convert.FromBase64String(ImgStr); 

     File.WriteAllBytes(imgPath, imageBytes); 

     return true; 
    } 

希望這將有助於未來。

尤其對於那些使用Asp.net來製作Api而不是PHP和其他語言的人。

+0

這不是一種傳輸圖像數據的非常有效的方法。 Base64將每組三個字節編碼爲四個字節,再加上一些額外的填充以確保它是4的倍數。這意味着大小爲n的字符串的base-64表示的大小爲:ceil(n/3)* 4或大約30%的額外大小。 –

+0

我想補充Brian的評論。您可以將原始圖像數據放入POST主體,然後在C#中使用context.Request.InputStream訪問原始數據。另一個需要注意的是許多服務器限制了請求URL的長度。所以如果你的圖像變得太大,你將被迫切換到這種方法。 – Licht