简体   繁体   English

android解析意图返回的文件路径

[英]android parse file path returned from an intent

I'll keep it as simple as possible. 我将使其尽可能简单。

I am working on an app, the app has check-lists that are to be submitted. 我正在开发一个应用程序,该应用程序具有要提交的检查清单。 Part of these check-lists require upload of files. 这些核对清单中的一部分要求上传文件。 I know how to serialize and upload files, but I need to get these files in the first place. 我知道如何序列化和上传文件,但是首先需要获取这些文件。

Code follows 代码如下

public void getFile(View v)
{
    Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
    intent.setType("file/*");
    startActivityForResult(intent, 1337);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    String Fpath;
    if(data!=null)
    {
        Fpath = data.getDataString();
        File test = new File(Fpath);
        if(test.exists())
            Log.v(LOG_TAG,"size: "+test.length());
        else
            Log.v(LOG_TAG,"does not exist:"+test.length());

    }
    else
    Fpath = "empty";
    Log.v(LOG_TAG,"Path: "+Fpath);
    Log.v(LOG_TAG,"Request Code: "+requestCode);
    Log.v(LOG_TAG, "Result Code: " + resultCode);
    super.onActivityResult(requestCode, resultCode, data);
}

The above code produces some interesting results. 上面的代码产生了一些有趣的结果。
The results are: 结果是:

Selecting an image<br>
does not exist:0<br>
Path: content://media/external/images/media/10857 <br>
Request Code: 1337 <br>
Result Code: -1<br>

Selecting a binary file<br>
does not exist:0<br>
Path: file:///storage/emulated/0/Download/ACSSE_IT00137_2015_LG.pdf <br>
Request Code: 1337 <br>
Result Code: -1 <br>

What am I doing wrong? 我究竟做错了什么?
I cannot read and serialize the file, do I have to parse the file differently? 我无法读取和序列化文件,是否必须以其他方式解析文件?
I know I can grab images differently but I also need binary files so please don't tell me to use the gallery app. 我知道我可以抓取不同的图像,但我也需要二进制文件,因此请不要告诉我使用Gallery应用程序。

Thanks in advance. 提前致谢。

ok I found what I missed 好,我找到了我错过的东西

The android intent returns an extra piece of information that works like intents are supposed to. android intent返回了一条额外的信息,该信息的工作原理与intent一样。
file:///storage/emulated/0/Download/ACSSE_IT00137_2015_LG.pdf 文件:///storage/emulated/0/Download/ACSSE_IT00137_2015_LG.pdf

The first 7 characters of the returned string is a file intent, or at least I think that is what it is. 返回的字符串的前7个字符是文件意图,或者至少我认为是这样。

String Fpath = data.getDataString();
String abspath = Fpath.substring(7);

File test = new File(abspath);
if(test.exists())
{
    //serialize it!
}

The file object now finds the file apropriatly 现在,文件对象可以正确找到文件

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

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