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

查看:18
本文介绍了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.

那该怎么办?

嗯,这取决于.

如果您只想接受文件,请集成文件选择器库而不是使用 ACTION_GET_CONTENT.(UPDATE 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:

  • 如果UrigetScheme()返回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天全站免登陆