jfversluis / FilePicker-Plugin-for-Xamarin-and-Windows

FilePicker Plugin for Xamarin and Windows
MIT License
157 stars 80 forks source link

Open file in SDcard? #183

Closed vik12J closed 4 years ago

vik12J commented 4 years ago

Expected Behavior

Open file in SDcard

Actual Behavior

File not found

Steps to Reproduce the Problem

var file = await CrossFilePicker.Current.PickFile();
string pathFile = file.FilePath;
using (var db = new SQLiteConnection(pathFile))
{
    var table = db.Query<TestTable>("SELECT id, Name  FROM TestTable");
    //- various further actions -//
    ............
}

If you open the base from the phone’s memory, then everything’s okay. When trying to open with a sdcard, an error. the path to the database file is like this: content://com.android.externalstorage.documents/document/1513-1812%3AtestBase.db P.S. I am a beginner and do not quite understand what to do with it :(

Specifications

jfversluis commented 4 years ago

Is the filename the same when you open it from your phone's memory?

Could you try a filename without any special characters. In your example I notice the %3A which is a colon (:). Just to see it that has anything to do with it.

vik12J commented 4 years ago

Is the filename the same when you open it from your phone's memory?

Yes

Could you try a filename without any special characters. In your example I notice the %3A which is a colon (:). Just to see it that has anything to do with it.

I tried differently

content://com.android.externalstorage.documents/document/1513-1812%3AtestBase.db
content://com.android.externalstorage.documents/document/1513-1812/testBase.db
content://com.android.externalstorage.documents/document/1513-1812:testBase.db
/storage/1513-1812/testBase.db
/storage/1513-1812%3AtestBase.db
/document/1513-1812:testBase.db

It does not work, the file is not available.

================================= If I refer to the file as a stream file.DataArray, then it is available, I can get for example the file size or open the image as a stream, but I won’t work with the Sqlite database as a stream, so I need to open the database through the path.

vividos commented 4 years ago

Note that SQLiteConnection wants to have a real file, not an URL that is returned in the FilePath property. The content:// may not even represent a real file. You have to copy the content to your local app data folder, e.g. using Stream.CopyTo() method.

vik12J commented 4 years ago

You have to copy the content to your local app data folder, e.g. using Stream.CopyTo() method.

I thought about it, that is, each with ssdcard copy to the application folder? And if the database, for example, is 300-500GB in size? This will be a long run each time waiting for copying, and the problem will be bad if there is no space in the local folder.

In addition, other applications somehow work directly with files at cdcard

vividos commented 4 years ago

The FilePicker tries hard to find out a real file path, and if it works, you get a file:// URL. In your case the Android content provide didn't return a real filename, so your only chance would be to copy t e file. You always can write a custom file picker, e.g. with Forms, to enumerate the files in a specific folder. Or you could check out the IOUtil.android.cs and figure out another way to get a real path. We're always welcome pull requests!

vik12J commented 4 years ago

The FilePicker tries hard to find out a real file path, and if it works, you get a file:// URL.

If FilePicker couldn't get the file path, how can I?:(

vividos commented 4 years ago

Well, your an Android developer, aren't you? And the original author of IOFile.android.cs was, too. It's not magic, it's Android knowledge and research :-)

vik12J commented 4 years ago

Well, your an Android developer, aren't you? And the original author of IOFile.android.cs was, too. It's not magic, it's Android knowledge and research :-)

Yes, I'm new to Android development . I'm trying to figure it out, I've looked at a lot of articles, but I haven't found the answer to my question anywhere. After all, developers of Android applications somehow get access to files on sdcard, audio and video players for example.

graysuit commented 4 years ago

@vik12J I had same issue and I solved it Remember to escape url. Path should not have %3A="/" ,%20=" " etc https://github.com/jfversluis/FilePicker-Plugin-for-Xamarin-and-Windows/issues/204

jfversluis commented 4 years ago

Closing this since this project will retire, please check out Xamarin.Essentials which now has a FilePicker based on this one.