0

I´m trying to access a file to send it via POST with Retrofit, but everytime and with any file i´m getting java.io.FileNotFoundException: /document/image:182 (No such file or directory) I´m asking permission in runtime, app is min API 23 (Marshmallow), I´m getting the uri fine (i guess), the uri.Path is correct (/document/image:182) but with diferent name ("file.jpg") I don´t know if this is ok or not.

the code

case R.id.adjuntar_archivo:
        if (this.requireContext().checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
             == PackageManager.PERMISSION_DENIED) {
            String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};
            requestPermissions(permissions, OPEN_PERMISSION);
        } else {
            openFile();
        }
        break;

private void openFile() {

    Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
    intent.setType("*/*");
    intent.addCategory(Intent.CATEGORY_OPENABLE);
    startActivityForResult(intent, OPEN_REQUEST_CODE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == OPEN_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
        if (data != null) {
            if (data.getData() != null) {
                uri = data.getData();
                //hasAttachment is for POST request with/without attached file
                hasAttachment = true;
            }
        }
    }
}
Alejandro
  • 9
  • 3
  • 1
    `the uri.Path is correct (/document/image:182) `,. No. And it is no path. Have a look at data.getData().toString() to see the obtained content scheme. – blackapps Jul 27 '20 at 20:36
  • Please remove all Spanish. – blackapps Jul 27 '20 at 20:36
  • Thanks on the edit, sorry for language. on data.getData.toString() got `java.io.FileNotFoundException: content:/com.android.providers.media.documents/document/image:182 (No such file or directory)` – Alejandro Jul 27 '20 at 21:51
  • A `Uri` is not a file. Use the `InputStreamRequestBody` from [here](https://commonsware.com/blog/2020/07/05/multipart-upload-okttp-uri.html) or [here](https://cketti.de/2020/05/23/content-uris-and-okhttp/). Or, use the second one from [this OkHttp issue comment](https://github.com/square/okhttp/issues/3585#issuecomment-327319196). – CommonsWare Jul 27 '20 at 22:47

0 Answers0