don/cordova-filechooser

Get actual file name

Opened this issue ยท 7 comments

Hey, I don't think this is a filechooser issue itself (thanks for the plugin by the way!) but I am struggling with copying a file to the app directory using the file chooser and keeping it's file name.

For example, I've got a file called PC-Plus.zip in my Downloads folder, when I select it with the file chooser, I get:

content://com.android.providers.downloads.documents/document/3634

and with this information I've not been able to retrieve the actual file name!

Are you able to point me in the right direction?

Cheers
-Chris

I have the same problem, could you fix it?

don commented

The last element of the path will be the filename when you get a file:// uri. When you get a content:// uri, it takes more work to get the metadata.

You'll need to query a content resolver and then grab the display name. See the example in document provider.

This would require modifications to the native code. You could add a method to get a filename from a URI or perhaps return an object literal in the original call.
{
uri: "content://com.android.providers.downloads.documents/document/3634",
name: "PC-Plus.zip"
}

I have editted code in FileChooser.java:64. We only need to get real file path (in file:// protocol) from external storage. See this: http://stackoverflow.com/a/20559175/4575459 -- there are all cases.

if ("content".equals(uri.getScheme()))
{
    String path = "";
    final String docId = DocumentsContract.getDocumentId(uri);
    final String[] split = docId.split(":");
    final String type = split[0];

    if ("primary".equalsIgnoreCase(type))
    {
        path = "file://" + Environment.getExternalStorageDirectory() + "/" + split[1];
    }

    callback.success(path);
}
else
{
    callback.success(uri.toString());
}

I Have the same issue.

Can you please suggest how can i resolve?

Is there any sample codes?

@petr001 Hi, thanks for your piece of code. Unfortunately does not seem to put anything in path because DocumentsContract.getDocumentId(uri) just returns a number.

Do you have any insight on this?

Use this to get the proper path: https://github.com/hiddentao/cordova-plugin-filepath
Then get the filename out of the path: filePath.split('/').pop()

Unfortunately that filepath plugin is no longer actively maintained.