Using the dialog box I recognize the Uri file.

Intent i = new Intent().setType("*/*").setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(i, "Выбор файла"), 123); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == 123 && resultCode == RESULT_OK) { Uri uri = data.getData(); .... File f = new File(uri.toString); // Далее с помощью OkHttp отправляю файл... } } 

In staktreys writes

java.io.FileNotFoundException: content: /com.android.providers.media.documents/document/image%3A10146 (No such file or directory)

If you replace uri.getPath() almost the same

java.io.FileNotFoundException: / document / image: 10146 (No such file or directory)

Given that the file that I open has the extension .png and a completely different name.

  • by extension - en.stackoverflow.com/a/631110/177345 . There is also a universal code for getting a file by URI (from which then we get the name and from the name - extension) - pavlofff

2 answers 2

First, not every Uri can be converted to a file system path. Specifically your probably possible.

Secondly, if there is an opportunity to work with the data stream, then it is easy to get it:

 InputStream fileStream = null; try { fileStream = getContentResolver().openInputStream(uri); } catch (IOException e) { Log.e(this.getClass().getName(), e.getMessage()); } 

If, after all, there is no way with the stream, then it’s harder, here’s an example for files from the gallery:

 public String getRealPathFromURI(Context context, Uri contentUri) { Cursor cursor = null; try { String[] proj = { MediaStore.Images.Media.DATA }; cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } finally { if (cursor != null) { cursor.close(); } } } 
  • Tell me, is it possible to somehow find out the file extension? - Danil
  • You can find out MIME , and on it already pick up the extension. - Eugene Krivenja
  • Thanks, it turned out to be the getContentResolver().getType(uri) method - Danil

I have such an implementation.

 InputStream inputStream = null; OutputStream outputStream = null; Intent i = new Intent().setType("*/*").setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(i, "Выбор файла"), 123); @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == 123 && resultCode == RESULT_OK) { Uri uri = data.getData(); .... File f = null; try { // read this file into InputStream inputStream = getContentResolver().openInputStream(uri); // write the inputStream to a FileOutputStream outputStream = new FileOutputStream(f); int read = 0; byte[] bytes = new byte[1024]; while ((read = inputStream.read(bytes)) != -1) { outputStream.write(bytes, 0, read); } // Далее с помощью OkHttp отправляем файл... } catch (IOException e) { e.printStackTrace(); } finally { if (inputStream != null) { try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); } } if (outputStream != null) { try { // outputStream.flush(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } } } } }