Ok so what I am trying to do is to open an image, set it in an imageview and make it a file in order to send it in a multipartform request using volley, right now I am using PostMan to make Http Requests to my server (string and file) and it is working properly but I can not implement it in android what I have so far is this
public void onClick(View v) {
if(v == banner){
Intent intent = new Intent();
//Verified the android version
if(Build.VERSION.SDK_INT < 19){
intent.setAction(Intent.ACTION_GET_CONTENT);
} else{
//KitKat 4.4 o superior
intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
}
intent.setType("image/*");
startActivityForResult(intent, request_code);
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == Activity.RESULT_OK && requestCode == request_code) {
uri = data.getData();
banner.setImageURI(uri);
//TAG for store the Uri to the selected file
banner.setTag(uri);
path = uri.getPath();
fileImage = new File (path);
Toast.makeText(this,path,Toast.LENGTH_LONG).show();}
Where banner is my imageview and fileImage is my file
MultipartRequest myRequest = new MultipartRequest(
"MY_URL",
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("error");
}
},
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
VolleyLog.e(response);
}
},
fileImage,
params
);
I am using this class that was answered in another question
public class MultipartRequest extends Request<String> {
//private MultipartEntity entity = new MultipartEntity();
MultipartEntityBuilder entity = MultipartEntityBuilder.create();
HttpEntity httpentity;
private static final String FILE_PART_NAME = "banner";
private final Response.Listener<String> mListener;
private final File mFilePart;
private final Map<String, String> mStringPart;
public MultipartRequest(String url, Response.ErrorListener errorListener,
Response.Listener<String> listener,
File file,
Map<String, String> mStringPart) {
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
this.mStringPart = mStringPart;
entity.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
buildMultipartEntity();
}
public void addStringBody(String param, String value) {
mStringPart.put(param, value);
}
private void buildMultipartEntity() {
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
for (Map.Entry<String, String> entry : mStringPart.entrySet()) {
entity.addTextBody(entry.getKey(), entry.getValue());
}
}
@Override
public String getBodyContentType() {
return httpentity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
httpentity = entity.build();
httpentity.writeTo(bos);
} catch (IOException e) {
VolleyLog.e("IOException writing to ByteArrayOutputStream");
}
return bos.toByteArray();
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
return Response.success("Uploaded", getCacheEntry());
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
}
But it return me the error "IOException writing to ByteArrayOutputStream"