cordova-filechooser icon indicating copy to clipboard operation
cordova-filechooser copied to clipboard

Get actual file name

Open chrismingay opened this issue 10 years ago • 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

chrismingay avatar Oct 23 '14 11:10 chrismingay

I have the same problem, could you fix it?

mrktrace avatar Jan 09 '15 14:01 mrktrace

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" }

don avatar Jan 09 '15 16:01 don

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());
}

petr001 avatar Feb 17 '15 13:02 petr001

I Have the same issue.

Can you please suggest how can i resolve?

Is there any sample codes?

ullasmohan avatar May 19 '15 08:05 ullasmohan

@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?

warrenjoe92 avatar Feb 04 '16 00:02 warrenjoe92

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()

filerun avatar Apr 08 '16 10:04 filerun

Unfortunately that filepath plugin is no longer actively maintained.

kukukk avatar Aug 30 '17 15:08 kukukk