Hi am working on a chat application and now i want to integrate share audio image PDF video from my application . and i am using this code but its not working properly. do anyone have any idea how to send these files using volley or retrofit. Hear is the which i tried.
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
intent.addCategory(Intent.CATEGORY_OPENABLE);
try {
startActivityForResult(
Intent.createChooser(intent, "Select a File to Upload"),
FILE_SELECT_CODE);
} catch (android.content.ActivityNotFoundException ex) {
// Potentially direct the user to the Market with a Dialog
Toast.makeText(this, "Please install a File Manager.",
Toast.LENGTH_SHORT).show();
}
And this is my on onActivityResult method.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
switch (requestCode) {
case FILE_SELECT_CODE:
if (resultCode == RESULT_OK) {
Uri uri = data.getData();
ContentResolver cr = this.getContentResolver();
mime = cr.getType(uri);
Toast.makeText(ChatActivity.this,mime,Toast.LENGTH_LONG).show();
Log.d(TAG, "File Uri: " + uri.toString()+" "+mime);
// Get the path
if(mime.equalsIgnoreCase("image/jpeg")){
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), uri);
uploadImage();
} catch (IOException e) {
e.printStackTrace();
}
}else{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
FileInputStream fis;
try {
fis = new FileInputStream(new File(uri.getPath()));
byte[] buf = new byte[1024];
int n;
while (-1 != (n = fis.read(buf)))
baos.write(buf, 0, n);
} catch (Exception e) {
e.printStackTrace();
}
filebytes = baos.toByteArray();
uploadImage();
}
}
break;
}
super.onActivityResult(requestCode, resultCode, data);
}
And this is my upload method.
private void uploadImage(){
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
try {
JSONObject jsonObject = new JSONObject(s);
String message = jsonObject.getString("result");
sendfileMessage(message);
} catch (JSONException e) {
e.printStackTrace();
}
Log.d("image path",s);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
//Dismissing the progress dialog
loading.dismiss();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
//Converting Bitmap to String
Map<String,String> params = new Hashtable<String, String>();
//Adding parameters
params.put("mkey", "R09fQ2hhdC0z");
if(mime.equalsIgnoreCase("image/jpeg")){
String image = getStringImage(bitmap);
params.put("ch_img", image);
}else{
String image = new String(filebytes);
params.put("ch_img", image);
}
//returning parameters
Log.d("imagedemo",params.toString());
return params;
}
};
//Creating a Request Queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//Adding request to the queue
requestQueue.add(stringRequest);
}
You can upload files to API with Mutipart Data.
Create an interface if you are using Retrofit like -
@Multipart
@POST("updateProfile")
Call<JsonObject> uploadFile(@Part MultipartBody.Part file);
Then create Mutipart Data :
private MultipartBody.Part getFilePart(String filePath) {
File fileToUpload=new File(filePath);
return MultipartBody.Part.createFormData("file", fileToUpload.getName(),fileToUpload);
}
At last just call the API
retrofitObject.uploadFile(getFilePart("filePath").enqueue(this);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.