【问题标题】:Unable to access file from uri无法从 uri 访问文件
【发布时间】:2020-11-29 03:39:24
【问题描述】:

我正在尝试使用存储在本地的存储访问框架访问文件并将其发送到服务器。但是当我尝试使用 URI 获取文件时,我会得到 NullPointerException。但是我得到了文件的 URI。但是通过获取路径转换为文件时捕获异常。 最低 API 为 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;
        }

【问题讨论】:

  • 你使用getPath(getContext(),uri)的方法是什么?哪个班的?
  • 抱歉更新了我的答案错过了部分代码

标签: android storage-access-framework


【解决方案1】:

我正在尝试使用存储在本地的存储访问框架访问文件并将其发送到服务器。

欢迎您的用户选择他们想要的任何内容,其中不包括您可以直接访问的文件(例如,在 Google 云端硬盘中、可移动存储上)。

但通过获取路径转换为文件时捕获异常

您不能“通过获取路径转换为文件”。 content Uri 的路径部分是一组无意义的字符,用于标识特定内容。接下来,您会认为所有计算机在其本地文件系统的路径/questions/43818723/unable-to-access-file-from-uri 上都有一个文件,因为https://stackoverflow.com/questions/43818723/unable-to-access-file-from-uri 恰好是一个有效的Uri

所以,摆脱getPath()

使用ContentResolveropenInputStream() 在内容上获得InputStream。直接使用该流或将其与您自己的文件上的 FileOutputStream 结合使用,以制作可用作文件的内容的本地副本。

【讨论】:

    【解决方案2】:

    @CommonsWare 答案是正确的 这是代码sn-p

    从 Uri 读取文件内容:

            // Use ContentResolver to access file from Uri
        InputStream inputStream = null;
        try {
            inputStream = mainActivity.getContentResolver().openInputStream(uri);
            assert inputStream != null;
    
            // read file content
            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
            String mLine;
            StringBuilder stringBuilder = new StringBuilder();
            while ((mLine = bufferedReader.readLine()) != null) {
                stringBuilder.append(mLine);
            }
            Log.d(TAG, "reading file :" + stringBuilder);
    

    将文件从 Uri 保存到应用程序目录中的本地副本:

    String dirPath = "/data/user/0/-your package name -/newLocalFile.name"
    
    try (InputStream ins = mainActivity.getContentResolver().openInputStream(uri)) {
    
                File dest = new File(dirPath);
    
                try (OutputStream os = new FileOutputStream(dest)) {
                    byte[] buffer = new byte[4096];
                    int length;
                    while ((length = ins.read(buffer)) > 0) {
                        os.write(buffer, 0, length);
                    }
                    os.flush();
          } catch (Exception e) {
                e.printStackTrace();
          }
    
      } catch (Exception e) {
             e.printStackTrace();
      }
    
                
    

    【讨论】:

    • 文件作为本地副本保存在应用程序目录后,是否可以重新启动应用程序并以编程方式从该文件中读取,而无需手动选择文件?
    • 好的,我现在明白如果文件保存到文件系统,例如在应用程序的内部存储中,则该文件存在并且可以在应用程序重新启动时读取(显然),但是之前运行的内容uri在应用程序重新启动时没有任何意义。
    【解决方案3】:

    尝试下面的代码获取路径:

    public String getPath(Uri uri) throws URISyntaxException {
        final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19;
        String selection = null;
        String[] selectionArgs = null;
        // Uri is different in versions after KITKAT (Android 4.4), we need to
        // deal with different Uris.
        if (needToCheckUri && DocumentsContract.isDocumentUri(mainActivity, uri)) {
            if (isExternalStorageDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            } else if (isDownloadsDocument(uri)) {
                final String id = DocumentsContract.getDocumentId(uri);
                uri = ContentUris.withAppendedId(
                        Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
            } else if (isMediaDocument(uri)) {
                final String docId = DocumentsContract.getDocumentId(uri);
                final String[] split = docId.split(":");
                final String type = split[0];
                if ("image".equals(type)) {
                    uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
                } else if ("video".equals(type)) {
                    uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
                } else if ("audio".equals(type)) {
                    uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
                }
                selection = "_id=?";
                selectionArgs = new String[] {
                        split[1]
                };
            }
        }
        if ("content".equalsIgnoreCase(uri.getScheme())) {
            String[] projection = {
                    MediaStore.Images.Media.DATA
            };
            Cursor cursor = null;
            try {
                cursor = mainActivity.getContentResolver()
                        .query(uri, projection, selection, selectionArgs, null);
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    return cursor.getString(column_index);
                }
            } catch (Exception e) {
    
            }
        } else if ("file".equalsIgnoreCase(uri.getScheme())) {
            return uri.getPath();
        }
        return null;
    }`/**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is ExternalStorageProvider.
     */
    public static boolean isExternalStorageDocument(Uri uri) {
        return "com.android.externalstorage.documents".equals(uri.getAuthority());
    }
    
    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is DownloadsProvider.
     */
    public static boolean isDownloadsDocument(Uri uri) {
        return "com.android.providers.downloads.documents".equals(uri.getAuthority());
    }
    
    /**
     * @param uri The Uri to check.
     * @return Whether the Uri authority is MediaProvider.
     */
    public static boolean isMediaDocument(Uri uri) {
        return "com.android.providers.media.documents".equals(uri.getAuthority());
    }`
    

    【讨论】:

    • 这比没用还糟糕。它对各种提供程序的内部实现做出假设,并且这些假设不必在 Android OS 版本甚至不同制造商的设备之间成立。它也不处理大多数 content Uri 值,例如来自使用 FileProvider 的应用程序的值。
    猜你喜欢
    • 1970-01-01
    • 2021-10-10
    • 2016-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多