onActivityResult的intent.getPath()没有给我正确的文件名 [英] onActivityResult's intent.getPath() doesn't give me the correct filename

查看:91
本文介绍了onActivityResult的intent.getPath()没有给我正确的文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试以这种方式获取文件:

I am trying to fetch a file this way:

final Intent chooseFileIntent = new Intent(Intent.ACTION_GET_CONTENT);
    String[] mimetypes = {"application/pdf"};
    chooseFileIntent.setType("*/*");
    chooseFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
    if (chooseFileIntent.resolveActivity(activity
                        .getApplicationContext().getPackageManager()) != null) {
        chooseFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimetypes);
        activity.startActivityForResult(chooseFileIntent, Uploader.PDF);
    }

然后在onActivityResult中:

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

根据许多线程,我应该使用data.getData().getPath()从意图中获取文件名,我期望的文件名是 my_file.pdf ,但是我得到的却是这个:

According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath(), the file name I'm expecting is my_file.pdf, but instead I'm getting this :

/document/acc = 1; doc = 28

/document/acc=1;doc=28

那该怎么办?感谢您的帮助.

So what to do? Thanks for your help.

推荐答案

我正在尝试获取文件

I am trying to fetch a file

没有该代码.该代码要求用户选择内容.这可能是文件,也可能不是.

Not with that code. That code is asking the user to pick a piece of content. This may or may not be a file.

根据许多线程,我应该使用data.getData().getPath()从意图中获取文件名

According to many threads I'm supposed to fetch the file name from the intent with data.getData().getPath()

尽管它倾向于在较旧的Android版本上运行,但这永远是不正确的.

That was never correct, though it tended to work on older versions of Android.

那该怎么办?

So what to do?

嗯,这取决于.

如果您只想接受文件,请集成文件选择器库 ACTION_GET_CONTENT.(更新2019-04-06 :由于Android Q禁止大多数文件系统访问,因此该解决方案不再可行)

If you wish to only accept files, integrate a file chooser library instead of using ACTION_GET_CONTENT. (UPDATE 2019-04-06: since Android Q is banning most filesystem access, this solution is no longer practical)

如果您愿意允许用户使用ACTION_GET_CONTENT选择一段内容,请理解它不必是文件,也不必具有类似于文件名的内容.您将获得的最接近的数字:

If you are willing to allow the user to pick a piece of content using ACTION_GET_CONTENT, please understand that it does not have to be a file and it does not have to have something that resembles a filename. The closest that you will get:

  • 如果Uri中的getScheme()返回file,则您原来的算法将起作用

  • If getScheme() of the Uri returns file, your original algorithm will work

如果UrigetScheme()返回content,请使用DocumentFile.fromSingleUri()创建DocumentFile,然后在该DocumentFile上调用getName()—这应该返回一个显示名称",用户应该可以识别

If getScheme() of the Uri returns content, use DocumentFile.fromSingleUri() to create a DocumentFile, then call getName() on that DocumentFile — this should return a "display name" which should be recognizable to the user

这篇关于onActivityResult的intent.getPath()没有给我正确的文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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