我正在尝试使用 Volley 发布我的数据但是我无法在服务器上上传我的图像 . 总是得到错误,如 http:\\www.mybaseurl.com/upload.php
的意外响应代码500 . 以下是我试图上传的代码
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
private void uploadImage(){
//Showing the progress dialog
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(MainActivity.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(MainActivity.this, ""+volleyError, Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
String image = getStringImage(bitmap);
//Getting Image Name
String name = editTextName.getText().toString().trim();
//Creating parameters
Map<String,String> params = new Hashtable<String, String>()
params.put("empsno", "81");
params.put("storesno", "165");
params.put("lrSno", "1808");
params.put("recQty", "0");
params.put("recVol", "0");
params.put("recWgt", "0");
params.put("damageQty", "0");
params.put("looseQty", "0");
params.put("deliveryDate", "2016-09-24");
params.put("deliveryTime", "10:15");
params.put("uploadFile", image);
params.put("remarks", "mytestingrem");
params.put("receivedBy", "amankumar");
params.put("ipAddress", "12.65.65.32");
//returning parameters
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onClick(View v) {
if(v == buttonChoose){
showFileChooser();
}
if(v == buttonUpload){
uploadImage();
}
}
请帮帮我,如何使用这些参数上传文件 . 我是凌空抽射的新手 . 我只是从 https://www.simplifiedcoding.net/android-volley-tutorial-to-upload-image-to-server
复制粘贴此代码 . 即使我没有正确使用't know whether i'm . Thanks in advance
2 回答
你应该必须理解使用凌空库和图像上传的概念 . 以下是图像上传和使用排球库的一些其他有用链接 .
volley library
Image upload using multipart
注意:我也测试过你的tutorial.code没问题 . 请正确检查您的图像路径 . 如果可能,那么在任何托管的Web服务器上使用他们的PHP代码并检查他们的json响应并交叉检查您通过服务器脚本的参数传递的参数..
您可以尝试这种方法,它实际上适用于ma项目 . 首先,您必须从库中选择图像,然后必须将其转换为字符串并通过齐射发送到服务器
打开图库并选择图像的方法
添加此方法 . 这里的图像实际上已发送到服务器 .
将图像编码为字符串的方法
使用凌空上传