//add perimissiom in mainfests
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
//interface
@Multipart
@POST("requests/11006/history")
fun sendMessageWithImg(
@Part("X-Device-UDID") RequestBody deviceUDID,
@Part("Authorization") RequestBody token,
@Part List<MultipartBody.Part> file1
): Call<ResponseBody>
//below code placed in your Activity java class inside of oncreate
chooseanimage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
// intent.putExtra(Intent.ACTION_PICK,MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Log.i("cameraa", "01");
}
startActivityForResult(Intent.createChooser(intent, "select"), 0);
}
});
if (Build.VERSION.SDK_INT < 23) {
//Do not need to check the permission
} else {
RequestMultiplePermission();
}
upload..setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
uploadalbum(fileUris);
});
//below code placed in your Activity java class outside of oncreate
private void RequestMultiplePermission() {
// Creating String Array with Permissions.
ActivityCompat.requestPermissions(Create_Project.this, new String[]
{
CAMERA,
WRITE_EXTERNAL_STORAGE
}, RequestPermissionCode);
}
@NonNull
private RequestBody createPartFromString(String descriptionString) {
return RequestBody.create(
okhttp3.MultipartBody.FORM, descriptionString);
}
@NonNull
private MultipartBody.Part prepareFilePart(String partName, Uri fileUri) {
// https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
// use the FileUtils to get the actual file by uri
// File file = new File(strImgaepath);
File file = FileUtils.getFile(this, fileUri);
// create RequestBody instance from file
RequestBody requestFile =
RequestBody.create(
MediaType.parse(getContentResolver().getType(fileUri)),
file
);
// MultipartBody.Part is used to send also the actual file name
return MultipartBody.Part.createFormData(partName, file.getName(), requestFile);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 0 && data != null) {
ClipData clipData = data.getClipData();
if (data.getClipData() != null) {
int count = data.getClipData().getItemCount();
int currentItem = 0;
while (currentItem < count) {
Uri imageUri = data.getClipData().getItemAt(currentItem).getUri();
//do something with the image (save it to some directory or whatever you need to do with it here)
currentItem = currentItem + 1;
Log.d("Uri Selected", imageUri.toString());
try {
// Get the file path from the URI
String path = FileUtils.getPath(this, imageUri);
Log.d("Multiple File Selected", path);
fileUris.add(imageUri);
// MyAdapter mAdapter = new MyAdapter(Create_Project.this, arrayList);
// listView.setAdapter(mAdapter);
} catch (Exception e) {
// Log.e(TAG, "File select error", e);
}
}
} else if (data.getData() != null) {
//do something with the image (save it to some directory or whatever you need to do with it here)
final Uri uri = data.getData();
Log.i("sara", "Uri = " + uri.toString());
try {
// Get the file path from the URI
final String path = FileUtils.getPath(this, uri);
Log.d("Single File Selected", path);
fileUris.add(uri);
if (fileUris != null) {
setimage.setVisibility(View.VISIBLE);
}
} catch (Exception e) {
Log.e("sara", "File select error", e);
}
}
}
}
//upolad image
private void uploadalbum(ArrayList<Uri> fileUris) {
pDialog = new ProgressDialog(this);
pDialog.show();
pDialog.setMessage("Loading");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
ApiGetPost service = ApiConstant.getMainUrl(getApplicationContext()).create(ApiGetPost.class);
List<MultipartBody.Part> parts = new ArrayList<>();
for (int i = 0; i < fileUris.size(); i++) {
parts.add(prepareFilePart("file_upload[]", fileUris.get(i)));
Log.i("sara", String.valueOf(fileUris.get(i)));
}
updateMyProfile = service.upload(createPartFromString(strtitle),
createPartFromString(""),
createPartFromString(""), parts);
updateMyProfile.enqueue(new Callback<ModelLogin>() {
@Override
public void onResponse(Call<ModelLogin> call, Response<ModelLogin> response) {
pDialog.dismiss();
ModelLogin user = response.body();
String sa = String.valueOf(user.getStatus());
String msg = user.getMessage();
if (sa.equals("true")) {
Toast.makeText(getApplicationContext(), "" + msg, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<ModelLogin> call, Throwable t) {
}
});
}