如何将URI转换为棉花糖中的真实路径? [英] How to convert URI to real path in marshmallow?

查看:133
本文介绍了如何将URI转换为棉花糖中的真实路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个图片URI,我想将此URI转换为真实路径。我看了很多答案,但没有一个对我有用。我正在使用marshmallow 6.0.1。图片URI是 content://com.android.providers.media.documents/document/image%3A52530

I have an image URI and I want to convert this URI to a real path. I've looked at lot of of answers but none worked for me. I'm using marshmallow 6.0.1. The image URI is content://com.android.providers.media.documents/document/image%3A52530.

代码:

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

    }


推荐答案

Uri 不是文件 Uri 不必表示您可以访问的文件系统上的文件。 Uri 可能指向以下内容:

A Uri is not a File. A Uri does not have to represent a file on the filesystem that you can access. The Uri might point to content that is:


  • 存储在可移动存储上,你无法访问

  • 存储在另一个应用程序的内部存储上,你无法访问

  • 以加密形式存储,其中 ContentProvider 需要解密它

  • 存储在SQLite数据库的BLOB列中,其中 ContentProvider 需要加载并提供它

  • 存储在云中,其中 ContentProvider 需要下载

  • 即时生成,此网页的方式

  • 依此类推

  • 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
  • stored in a BLOB column of a SQLite database, where a ContentProvider needs to load it and serve it
  • stored in "the cloud", where a ContentProvider needs to download it
  • generated on the fly, the way this Web page is
  • and so on

您可以使用 ContentResolver openInputStream()来获取 InputStream 表示由 Uri 表示的内容。您可以在您控制的某个文件上创建 FileOutputStream 。并且,您可以使用Java I / O从 InputStream 复制到 OutputStream ,制作您自己的副本您控制的文件中的内容。

You can use ContentResolver and openInputStream() to get an InputStream on the content represented by the Uri. You can create a FileOutputStream on some file that you control. 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.

这篇关于如何将URI转换为棉花糖中的真实路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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