我正在尝试将手机拍摄的图像发送到服务器,当我尝试上传从手机拍摄的照片时,应用程序下方的代码崩溃了,我知道代码有效,因为我能够上传我拥有的图像下载到我的手机上。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
Uri selectedImage = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
String fileNameSegments[] = picturePath.split("/");
fileName = fileNameSegments[fileNameSegments.length - 1];
Bitmap myImg = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// Must compress the Image to reduce image size to make upload easy
myImg.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byte_arr = stream.toByteArray();
// Encode Image to String
encodedString = Base64.encodeToString(byte_arr, 0);
}
}
上传的代码
public void uploadImage() {
RequestQueue rq = Volley.newRequestQueue(this);
String url = "http://serverwebsite.com/image.php";
Log.d("URL", url);
StringRequest stringRequest = new StringRequest(Request.Method.POST,
url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.e("RESPONSE", response);
JSONObject json = new JSONObject(response);
Toast.makeText(getBaseContext(),
"The image is upload", Toast.LENGTH_SHORT)
.show();
} catch (JSONException e) {
Log.d("JSON Exception", e.toString());
Toast.makeText(getBaseContext(),
"Error while loadin data!",
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR", "Error [" + error + "]");
Toast.makeText(getBaseContext(),
"Cannot connect to server", Toast.LENGTH_LONG)
.show();
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("image", encodedString);
params.put("filename", fileName);
return params;
}
};
rq.add(stringRequest);
}
答案 0 :(得分:0)
我希望此代码可以帮助您
public void sendImage(MediaType mediaType)
{
OkHttpClient client = new OkHttpClient();
RequestBody requestBody = new MultipartBuilder()
.type(MultipartBuilder.FORM)
.addPart(
Headers.of("Content-Disposition",
"form-data; name=\"file\"; filename=" + '"'
+ name + '"'),
RequestBody.create(mediaType, new File(getFileAdress()
))).build();
final String credential = Credentials.basic(userName, pass);
Request request = new Request.Builder()
.header("Authorization", credential)
.url(WebServiceAddress())
.post(requestBody).build();
try {
Response response = client.newCall(request).execute();
int code=response.code();
String out = response.body().string();
if (response.isSuccessful()) {
} else {
} catch (Exception ex) {
}
}
这是我用来调整照片大小的库地址: https://github.com/alireza-87/ImageHelper