android - Get Path of Gallery Image Xamarin? -
i trying path of gallery image. getting path of image stored in internal storage not of external storage. have enabled read-write storage , camera access permissions has been granted. here code
void choosephoto() { try { var imageintent = new intent(); imageintent.settype("image/*"); imageintent.setaction(intent.actiongetcontent); startactivityforresult(intent.createchooser(imageintent, "select photo"), 0); } catch (exception ex) { throw ex; } } protected override void onactivityresult(int requestcode, result resultcode, intent data) { base.onactivityresult(requestcode, resultcode, data); if (requestcode == camera_capture) { // camera operation } else { console.writeline("choose pic gallery"); var uri = data.data; var path = getrealpathfromuri(uri); //path returns null when select image external storage; } } private string getrealpathfromuri(uri contenturi) { string[] proj = { mediastore.images.imagecolumns.data }; string result; var cursor = contentresolver.query(contenturi, proj, null, null, null); if (cursor == null) result = contenturi.path; else { int idx= cursor.getcolumnindex(mediastore.images.imagecolumns.data); cursor.movetofirst(); result = cursor.getstring(idx); cursor.close(); } return result; }
i want selected image path have choose gallery
the problem change android.net.uri path, here solution :
private string getpathtoimage(android.net.uri uri) { string doc_id = ""; using (var c1 = contentresolver.query(uri, null, null, null, null)) { c1.movetofirst(); string document_id = c1.getstring(0); doc_id = document_id.substring(document_id.lastindexof(":") + 1); } string path = null; // projection contains columns want return in our query. string selection = android.provider.mediastore.images.media.interfaceconsts.id + " =? "; using (var cursor = contentresolver.query(android.provider.mediastore.images.media.externalcontenturi, null, selection, new string[] { doc_id }, null)) { if (cursor == null) return path; var columnindex = cursor.getcolumnindexorthrow(android.provider.mediastore.images.media.interfaceconsts.data); cursor.movetofirst(); path = cursor.getstring(columnindex); } return path; } when choose picture gallery :
choosephoto(); ... protected override void onactivityresult(int requestcode, result resultcode, intent data) { base.onactivityresult(requestcode, resultcode, data); if (requestcode == 0) { var uri = data.data; var path = getpathtoimage(uri); system.diagnostics.debug.writeline("image path == " + path ); //my result ==> [0:] image path == /storage/emulated/0/dcim/camera/img_20170813_223324.jpg } } most important, when use method, maybe come across problem :
java.lang.securityexception: permission denial: reading com.android.providers.media.mediaprovider uri content://media/external/images/media pid=21975, uid=10417 requires android.permission.read_external_storage, or granturipermission()
when api >= 23, requesting permissions @ run time, users grant permissions apps while app running, not when install app. should check if have android.permission.read_external_storage permission, if not, need request permissions.
Comments
Post a Comment