无法使用"ACTION_GET_CONTENT"的Uri创建文件 [英] Cannot create a file using the Uri of `ACTION_GET_CONTENT`

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

问题描述

我正在尝试从设备中选择pdf文件并将其上传到服务器. ACTION_GET_CONTENT用于从设备中选择pdf.

I'm trying to select pdf file from the device and upload them to the server. ACTION_GET_CONTENT is used to select pdf from the device.

sel_book.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("application/pdf");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select PDF"), 1);
        }
    });

关于活动结果,我得到Uri并将其另存为字符串.

On activity result I get the Uri and save it as a String.

public void onActivityResult(int requestCode, int resultCode, Intent result) {
    if (resultCode == RESULT_OK) {
        if (requestCode == 1) {
            Uri uri = result.getData();
            String uriString = uri.toString();
            File myFile = new File(uriString);
            path = myFile.getAbsolutePath();
        }
    }
}

其结果路径为/document/primary:Download/Aptitude_2016_17.pdf.我需要使用它来创建一个新文件. File selectedFile = new File(selectedFilePath);.但是它不会创建文件. selectedFile.isFile()返回false.我不知道为什么会这样.请帮我解决这个问题.预先感谢.

It results the path as, /document/primary:Download/Aptitude_2016_17.pdf. I need to use this to create a new file. File selectedFile = new File(selectedFilePath);. But it doesn't create File. selectedFile.isFile() returns false. I have no idea why is it. Please help me to solve this. Thanks in advance.

推荐答案

ACTION_GET_CONTENT用于从设备中选择pdf.

ACTION_GET_CONTENT is used to select pdf from the device.

这允许用户选择一条内容.它不必是文件.

This allows the user to select a piece of content. It does not have to be a file.

关于活动结果,我得到Uri并将其另存为字符串.

On activity result I get the Uri and save it as a String.

这不是您使用Uri的方式.

其结果路径为/document/primary:Download/Aptitude_2016_17.pdf.

It results the path as, /document/primary:Download/Aptitude_2016_17.pdf.

这不是文件系统路径.这是具有content方案的Uri的一部分.您没有从ACTION_GET_CONTENT获取文件.您会得到一个Uri指向内容的一部分. Uri可以指向用户和另一个应用选择的任何内容:

That is not a filesystem path. That is a part of a Uri that has a content scheme. You do not get a file from ACTION_GET_CONTENT. You get a Uri that points to a piece of content. That Uri could point to anything that the user and the other app choose:

  • 您可以使用file方案通过Uri访问的文件
  • 一个文件,但是无法访问(例如,在另一个应用程序的内部存储上)
  • 数据库中BLOB列的内容
  • 需要下载的内容
  • 依此类推
  • A file that you can access, via a Uri with a file scheme
  • A file, but one that you cannot access (e.g., on internal storage of another app)
  • The contents of a BLOB column in the database
  • A piece of content that needs to be downloaded
  • And so on

使用ContentResovleropenInputStream()可以获取有关内容的流.直接使用该文件(与用于上传此内容的任何文件一起使用),或使用该流使用该内容的副本制作自己的文件,以便拥有一个可以使用的文件.

Use ContentResovler and openInputStream() to get a stream on whatever the content is. Either use that directly (with whatever you are using to upload this content), or use that stream to make your own file with a copy of that content, so that you have a file that you can use.

这篇关于无法使用"ACTION_GET_CONTENT"的Uri创建文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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