android - get path of video file selected from gallery getting NULL. How to get path of video file? -
get path of video file selected gallery getting null
. how path of video file ? get uri in activity result give null. converting uri string getting null
.
intent intent; string selectedvideo1path, selectedvideo2path; edittext e1,e2; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.videos_activity); button button1 = (button) findviewbyid(r.id.video1_btn); button button2 = (button) findviewbyid(r.id.video2_btn); button1.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { selectvideofromgallery(); startactivityforresult(intent, 101); } }); button2.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { selectvideofromgallery(); startactivityforresult(intent, 102); } }); } @override protected void onactivityresult(int requestcode, int resultcode, intent data) { if (requestcode == 101) { if (data.getdata() != null) { selectedvideo1path = getpath(data.getdata()); toast.maketext(mergevideosactivity.this, "path 1 : "+selectedvideo1path, toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "failed select video", toast.length_long).show(); } } if (requestcode == 102) { if (data.getdata() != null) { selectedvideo2path = getpath(data.getdata()); //string str2 = selectedvideo2path.tostring(); // e2.settext(str2); toast.maketext(mergevideosactivity.this, "path 2 : "+selectedvideo2path, toast.length_short).show(); } else { toast.maketext(getapplicationcontext(), "failed select video", toast.length_long).show(); } } }
this getpath method
public string getpath(uri uri) { int column_index = 0; string[] projection = {mediastore.images.media.data}; cursor cursor = managedquery(uri, projection, null, null, null); if (cursor != null) { column_index = cursor.getcolumnindexorthrow(mediastore.video.media.data); cursor.movetofirst(); } return cursor.getstring(column_index); }
selected video gallery hope ok ? check it
public void selectvideofromgallery() { if (android.os.environment.getexternalstoragestate().equals(android.os.environment.media_mounted)) { intent = new intent(intent.action_pick, android.provider.mediastore.video.media.external_content_uri); } else { intent = new intent(intent.action_pick, android.provider.mediastore.video.media.internal_content_uri); } intent.settype("video/*"); intent.setaction(intent.action_get_content); } }
update getpath method
public string generatepath(uri uri,context context) { string filepath = null; final boolean iskitkat = build.version.sdk_int >= build.version_codes.kitkat; if(iskitkat){ filepath = generatefromkitkat(uri,context); } if(filepath != null){ return filepath; } cursor cursor = context.getcontentresolver().query(uri, new string[] { mediastore.mediacolumns.data }, null, null, null); if (cursor != null) { if (cursor.movetofirst()) { int columnindex = cursor.getcolumnindexorthrow(mediastore.mediacolumns.data); filepath = cursor.getstring(columnindex); } cursor.close(); } return filepath == null ? uri.getpath() : filepath; } @targetapi(19) private string generatefromkitkat(uri uri,context context){ string filepath = null; if(documentscontract.isdocumenturi(context, uri)){ string wholeid = documentscontract.getdocumentid(uri); string id = wholeid.split(":")[1]; string[] column = { media.data }; string sel = media._id + "=?"; cursor cursor = context.getcontentresolver(). query(media.external_content_uri, column, sel, new string[]{ id }, null); int columnindex = cursor.getcolumnindex(column[0]); if (cursor.movetofirst()) { filepath = cursor.getstring(columnindex); } cursor.close(); } return filepath; }
Comments
Post a Comment