目前正在使用Volley
将图像
上传到服务器,但图像上传时为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;
}
}
关于java - 图片上传 android Java + Asp.net (C#),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42485592/