1

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();
    }
}
Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98
mishti
  • 183
  • 7
  • 20

1 Answers1

0

As far as I can assume, you are calling the requestPermissions function from a Fragment. As you are using ActivityCompat.requestPermissions for asking permission from the user, the onRequestPermissionsResult is not being invoked in the Fragment. Here is a similar problem which indicates the Fragment's onRequestPermissionsResult not being called and the calling Activity's onRequestPermissionsResult is 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

requestPermissions(new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);

instead of

ActivityCompat.requestPermissions(getActivity(), new String[]{Manifest.permission.CAMERA}, STORAGE_PERMISSION_CODE);

Do not use ActivityCompat.requestPermissions in your Fragment, as this will invoke the onRequestPermissionResult of your calling Activity which is launching the Fragment.

Hope that helps.

Reaz Murshed
  • 23,691
  • 13
  • 78
  • 98