简体   繁体   English

如何在棉花糖中将URI转换为真实路径?

[英]How to convert URI to real path in marshmallow?

I have an image URI and I want to convert this URI to a real path. 我有一个图像URI,我想将此URI转换为真实路径。 I've looked at lot of of answers but none worked for me. 我看了很多答案,但没有一个对我有用。 I'm using marshmallow 6.0.1. 我正在使用棉花糖6.0.1。 The image URI is content://com.android.providers.media.documents/document/image%3A52530 . 图像URI为content://com.android.providers.media.documents/document/image%3A52530

Code: 码:

   sendImageFromFolder.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                     Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true);
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"), 1);

                }
            });

     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);

     if(requestCode==1) {
          Uri uri = data.getData();//how to convert this uri to real path .
    }

    }

A Uri is not a File . Uri不是File A Uri does not have to represent a file on the filesystem that you can access. Uri不必代表您可以访问的文件系统上的文件。 The Uri might point to content that is: Uri可能指向以下内容:

  • stored on removable storage, which you cannot access 存储在无法访问的可移动存储中
  • stored on internal storage of another app, which you cannot access 存储在另一个应用程序的内部存储中,您无法访问
  • stored in an encrypted form, where a ContentProvider needs to decrypt it 以加密形式存储, ContentProvider需要对其进行解密
  • stored in a BLOB column of a SQLite database, where a ContentProvider needs to load it and serve it 存储在SQLite数据库的BLOB列中, ContentProvider需要在该列中加载并提供服务
  • stored in "the cloud", where a ContentProvider needs to download it 存储在“云”中, ContentProvider需要在此下载它
  • generated on the fly, the way this Web page is 即时生成的网页的方式
  • and so on 等等

You can use ContentResolver and openInputStream() to get an InputStream on the content represented by the Uri . 您可以使用ContentResolveropenInputStream()Uri表示的内容上获取InputStream You can create a FileOutputStream on some file that you control. 您可以在您控制的某个文件上创建FileOutputStream And, you can use Java I/O to copy from the InputStream to the OutputStream , making your own copy of the content in a file that you control. 而且,您可以使用Java I / O从InputStream复制到OutputStream ,在您控制的文件中创建内容的自己的副本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM