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.

Thanks in advance.

Answers


ok I found what I missed

The android intent returns an extra piece of information that works like intents are supposed to. file:///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.

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


Need Your Help

NHibernate and Windsor assembly conflicts

.net nhibernate orm castle-windsor assembly-resolution

I am having an issue getting Hibernate and Windsor to work together. My setup details are as follows

Objective C ARC equivalent for self?

iphone objective-c ios automatic-ref-counting xcode4.3

What is the equivalent keyword i can use in the place of self in ARC enabled projects?