xamarin.android - Reading an android file with Xamarin -
this super simple i'm having trouble reading bytes file. have working fine ios android crashes directorynotfoundexception
. think uri wrong i'm not sure i'm wrong it...
my view has button opens file picker...
var selecteddocumentpath = await dependencyservice.get<ifilepicker>().pickdocument();
my filepicker class...
class filepicker : ifilepicker { public async task<string> pickdocument() { string fileuri = string.empty; console.writeline("selecting document android device."); intent intent = new intent(intent.actionopendocument); intent.settype("*/*"); intent.putextra(intent.extramimetypes, new string[] { "application/msword", "application/vnd.openxmlformats-officedocument.wordprocessingml.document" }); intent.addcategory(intent.categoryopenable); ((activity)forms.context).startactivityforresult(intent.createchooser(intent, "select document"), mainactivity.document_code); return await getdocumentpath(new cancellationtokensource()); } private async task<string> getdocumentpath(cancellationtokensource tokensource) { var path = string.empty; var loopcheck = true; while (loopcheck) { (var = 0; < 100; i++) { if (mainactivity.fileuri != null || mainactivity.cancelled) { loopcheck = false; tokensource.cancel(); } try { await task.delay(1000, tokensource.token); } catch (taskcanceledexception) { break; } } } if (!mainactivity.cancelled) { path = mainactivity.fileuri.originalstring; } mainactivity.fileuri = null; mainactivity.cancelled = false; return path; } }
the onactivityresult
method located in mainactivity.cs
....
protected override void onactivityresult(int requestcode, result resultcode, intent data) { base.onactivityresult(requestcode, resultcode, data); if (resultcode == result.ok && requestcode == document_code) { fileuri = new uri(data.data.path); } else if(resultcode == result.canceled) { cancelled = true; } }
some paths i'm getting...
data.datastring = content://com.android.externalstorage.documents/document/primary%3adownload%2fsampleresume%20(1).docx data.data.schemespecificpart = //com.android.externalstorage.documents/document/primary:download/sampleresume (1).docx data.data.path = /document/primary:download/sampleresume (1).docx
and encoded options well. of these give me error later on when try , read using ...
file.readallbytes(selecteddocumentpath);
edit: i've replaced fileuri = new uri(data.data.path);
with
var uri = documentscontract.builddocumenturi(data.data.authority, documentscontract.getdocumentid(data.data)); fileuri = new uri(uri.path);
but still come out same type of path /document/primary:download/sampleresume (1).docx
Comments
Post a Comment