簡體   English   中英

圖片上傳android Java + Asp.net(C#)

[英]Image Upload android Java + Asp.net (C#)

當前正在使用VolleyImage上傳到服務器,但是圖像以0kb上傳,甚至沒有名稱,我從android上傳圖像的方式,首先將位圖轉換為String然后,服務器端的C#代碼變為返回Bitmap的字符串,下面是我的java代碼:

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

您僅在請求中發送圖像的路徑。 無法從您的服務器訪問此路徑。 我也不建議使用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;
    }
}

最終,我自己得到了答案,我必須在C#代碼中請求Android Java Volley Library

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

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

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

由於來自android的數據已參數化,因此您需要Request。

注意:

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而不是PHP和其他語言來制作Api的人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM