Android - How to get selected file name from the document -


i launching intent selecting documnets using following code.

private void showfilechooser() {     intent intent = new intent(intent.action_get_content);     intent.settype("*/*");     intent.addcategory(intent.category_openable);      try {         startactivityforresult(                 intent.createchooser(intent, "select file upload"), 1);     } catch (android.content.activitynotfoundexception ex) {         // potentially direct user market dialog         toast.maketext(this, "please install file manager.",                 toast.length_short).show();     } } 

in onactivity results when trying file path giving other number in place of file name.

    @override protected void onactivityresult(int requestcode, int resultcode, intent data) {     switch (requestcode) {     case 1:         if (resultcode == result_ok) {             // uri of selected file             uri uri = data.getdata();             file myfile = new file(uri.tostring());             string path = myfile.getabsolutepath();         }         break;     }     super.onactivityresult(requestcode, resultcode, data); } 

that path value getting this. "content://com.android.providers.downloads.documents/document/1433" want real file name doc1.pdf etc.. how it?

when content:// uri, you'll need query content resolver , grab display name.

@override protected void onactivityresult(int requestcode, int resultcode, intent data) {     switch (requestcode) {     case 1:         if (resultcode == result_ok) {             // uri of selected file             uri uri = data.getdata();             string uristring = uri.tostring();             file myfile = new file(uristring);             string path = myfile.getabsolutepath();             string displayname = null;              if (uristring.startswith("content://")) {                                    cursor cursor = null;                 try {                                                cursor = getactivity().getcontentresolver().query(uri, null, null, null, null);                                              if (cursor != null && cursor.movetofirst()) {                                                        displayname = cursor.getstring(cursor.getcolumnindex(openablecolumns.display_name));                     }                 } {                     cursor.close();                 }             } else if (uristring.startswith("file://")) {                            displayname = myfile.getname();             }         }         break;     }     super.onactivityresult(requestcode, resultcode, data); } 

Comments

Popular posts from this blog

c++ - No viable overloaded operator for references a map -

java - Custom OutputStreamAppender not run: LOGBACK: No context given for <MYAPPENDER> -

java - Cannot secure connection using TLS -