无法从uri访问文件 [英] Unable to access file from uri

查看:186
本文介绍了无法从uri访问文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用存储在本地的Storage Access Framework访问文件并将其发送到服务器.但是,每当我尝试使用 URI 获取文件时,都会得到 NullPointerException .但是我得到文件的URI.但是在通过获取路径转换为文件时捕获异常. 最低API为17

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

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;
        }

推荐答案

我正在尝试使用存储在本地的Storage Access Framework访问文件并将其发送到服务器.

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

欢迎您的用户选择所需的任何内容,其中不包含您可以直接访问的文件(例如,在Google云端硬盘中的可移动存储设备中).

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

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

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.

因此,请摆脱getPath().

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

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.

这篇关于无法从uri访问文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆