Search code examples
androidstorage-access-framework

Unable to access file from uri


I am trying to access a file using Storage Access Framework which I have stored in locally and send it to server. but when ever I try to get file using URI I get NullPointerException. However I get the URI of file. but catches exception when converting to file by getting path. Minimum API is 17

uriString = content://com.android.providers.downloads.documents/document/349

     warantyButton.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(Intent. ACTION_OPEN_DOCUMENT );
                    intent.addCategory(Intent.CATEGORY_OPENABLE);
                    intent.setType("*/*");
                    Intent i = Intent.createChooser(intent, "File");
                    getActivity().startActivityForResult(i, FILE_REQ_CODE);
                   //Toast.makeText(getContext(),"Files",Toast.LENGTH_SHORT).show();
                }
            });


     @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (requestCode == FILE_REQ_CODE) {
                if (resultCode == RESULT_OK) {
                    String path="";
                    Uri uri = data.getData();
                    if (uri != null) {
                        try {
                            file = new File(getPath(getContext(),uri));
                            if(file!=null){
                                ext = getMimeType(uri);
                                sendFileToServer(file,ext);
                            }

                        } catch (Exception e) {
                            Toast.makeText(getContext(),getString(R.string.general_error_retry),Toast.LENGTH_SHORT).show();
                            e.printStackTrace();
                        }
                    }
                }

            }

        }



public static String getPath(Context context, Uri uri) throws URISyntaxException {
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = { "_data" };
            Cursor cursor = null;

            try {
                cursor = context.getContentResolver().query(uri, projection, null, null, null);
                int column_index = cursor.getColumnIndexOrThrow("_data");
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
                // Eat it
            }
        }
        else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }

            return null;
        }

Solution

  • I am trying to access a file using Storage Access Framework which I have stored in locally and send it to server.

    Your users are welcome to choose anything they want, which does not include files that you can access directly (e.g., in Google Drive, on removable storage).

    but catches exception when converting to file by getting path

    You cannot "convert to file by getting path". The path portion of a content Uri is a meaningless set of characters that identifies the particular piece of content. Next, you will think that all computers have a file on their local filesystem at the path /questions/43818723/unable-to-access-file-from-uri, just because https://stackoverflow.com/questions/43818723/unable-to-access-file-from-uri happens to be a valid Uri.

    So, get rid of getPath().

    Use ContentResolver and openInputStream() to get an InputStream on the content. Either use that stream directly or use it in conjunction with a FileOutputStream on your own file, to make a local copy of the content that you can use as a file.