Android FilePicker dialog only working for some devices -
i'm using regular boiler-plate code showing file picker dialog on android. after file path, i'm uploading file server using aquery. however, code working on old samsung phone runs on android 4.1.2 not on other devices. tested using many devices works on samsung devices jellybean.
intent intent = new intent(intent.action_get_content); intent.settype(minmetype); intent.addcategory(intent.category_openable); // special intent samsung file manager intent sintent = new intent("com.sec.android.app.myfiles.pick_data"); // if want file type, can skip next line sintent.putextra("content_type", minmetype); sintent.addcategory(intent.category_default); intent chooserintent; if (getpackagemanager().resolveactivity(sintent, 0) != null) { // device samsung file manager chooserintent = intent.createchooser(sintent, "open file"); chooserintent.putextra(intent.extra_initial_intents, new intent[]{intent}); } else { chooserintent = intent.createchooser(intent, "open file"); } try { startactivityforresult(chooserintent, pickfile_request_code); } catch (android.content.activitynotfoundexception ex) { toast.maketext(getapplicationcontext(), "no suitable file manager found.\nplease download file manager , try again.", toast.length_long).show(); }
the onactivityresult code is:
protected void onactivityresult(int requestcode, int resultcode, intent data) { if (data != null) { fpath = data.getdatastring(); file file = new file(fpath.substring(7)); params.put("file" + nofilesattached, file); nofilesattached = nofilesattached + 1; path.settext(nofilesattached + " files attached "); upload.settext("upload (" + nofilesattached + ")"); upload.setvisibility(view.visible); isuploaded = false; } super.onactivityresult(requestcode, resultcode, data); }
i tried editing fpath.substring(7) fpath no success, , don't want use third-party libraries.
thanks in advance.
data.getdata()
return uri
; u need filepath; try find way transform uri
filepath: possibly:
public static string getrealfilepath( final context context, final uri uri ) { if ( null == uri ) return null; final string scheme = uri.getscheme(); string data = null; if ( scheme == null ) data = uri.getpath(); else if ( contentresolver.scheme_file.equals( scheme ) ) { data = uri.getpath(); } else if ( contentresolver.scheme_content.equals( scheme ) ) { cursor cursor = context.getcontentresolver().query( uri, new string[] { imagecolumns.data }, null, null, null ); if ( null != cursor ) { if ( cursor.movetofirst() ) { int index = cursor.getcolumnindex( imagecolumns.data ); if ( index > -1 ) { data = cursor.getstring( index ); } } cursor.close(); } } return data; }
Comments
Post a Comment