I am uploading any type of file from my code but the problem is when I run my code for the first time and try to upload an image, the image not being uploaded in that time. If I upload a file first, e.g. a PDF file, it is being uploaded successfully and after that when I upload an image, it is uploaded successfully as well. I don't understand anything about the problem. Please help.
Here is the code for uploading an image/file.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED) {
showFileChoser();
} else {
if (ActivityCompat.shouldShowRequestPermissionRationale(getActivity(), Manifest.permission.CAMERA)) {
Toast.makeText(getActivity(), "App requires Phone permission.\nPlease allow that in the device settings.", Toast.LENGTH_LONG).show();
}
ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);
}
} else {
showFileChoser();
}
And the showFileChoser function.
private void showFileChoser() {
Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select document"), PICK_DOCUMENT);
}
The onActivityResult.
// Handling the ima chooser activity result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PDF_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
filePath = data.getData();
// uploadMultipart();
try {
execMultipartPost();
} catch (Exception e) {
e.printStackTrace();
}
}
}
And onRequestPermissionsResult is...
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
//Checking the request code of our request
if (requestCode == STORAGE_PERMISSION_CODE) {
//If permission is granted
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
//Displaying a toast
Toast.makeText(getActivity(), "Permission granted now you can read the storage", Toast.LENGTH_LONG).show();
} else {
//Displaying another toast if permission is not granted
Toast.makeText(getActivity(), "Oops you just denied the permission", Toast.LENGTH_LONG).show();
}
}
}
The file uploading function is the following.
private void execMultipartPost() throws Exception {
RequestBody requestBody;
pBar.setVisibility(View.VISIBLE);
final SessionManager session = new SessionManager(getActivity());
final HashMap<String, String> loggedDetail = session.getLoggedDetail();
HashMap<String, String> params = new HashMap<String, String>();
String api_token = loggedDetail.get("api_token");
if (filePath != null) {
String path = FilePath.getPath(getActivity(), filePath);
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("identity_file", path)
.addFormDataPart("training", edtTraining.getText().toString())
.addFormDataPart("user_id", loggedDetail.get("id"))
.addFormDataPart("experience", experience)
.addFormDataPart("skills", edtSkills.getText().toString())
.addFormDataPart("address_file", "")
.addFormDataPart("cv_file", "")
.addFormDataPart("dbs_file", "")
.build();
}
}
As far as I can assume, you are calling the
requestPermissionsfunction from aFragment. As you are usingActivityCompat.requestPermissionsfor asking permission from the user, theonRequestPermissionsResultis not being invoked in theFragment. Here is a similar problem which indicates theFragment'sonRequestPermissionsResultnot being called and the callingActivity'sonRequestPermissionsResultis being called instead. I assume this is what happens in your case as well.Now to get rid of this problem, you need to use
instead of
Do not use
ActivityCompat.requestPermissionsin yourFragment, as this will invoke theonRequestPermissionResultof your callingActivitywhich is launching theFragment.Hope that helps.