Novice, write wrong also look correct!
In Android app development, you will often encounter the need to open the System File Manager to select a file and return to the path after the operation. For example, click on an Import button, first in the root directory to find the required files, if the file does not exist in the pop-up dialog box to select a file, select a file to return the file path, to register the button monitor:
public void OnClick (View arg0) {
if (Mfilepath.equals ("No relevant file found")) {
Alertdialog.builder Builder = new Alertdialog.builder (feildlistactivity.this);
Builder.setcancelable (FALSE);
Builder.settitle ("hint")
. setmessage ("The file does not exist in this directory are you looking for it from another directory?") ")
. Setpositivebutton ("Yes", new Dialoginterface.onclicklistener () {
@Override
public void OnClick (
Dialoginterface Dialog,int which) {
Open the System file browsing feature
Intent Intent = new Intent ();
Intent.setaction (intent.action_get_content);
Intent.settype ("*/*");
Intent.addcategory (intent.category_openable);
Startactivityforresult (Intent,infile_code);
}
})
. Setnegativebutton ("No", null})
. Show ();
}
To override the Onactivityresult function, get the return path inside the function, with the following code:
protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
if (resultcode! = ACTIVITY.RESULT_OK) {
Finish ();
} else if (Requestcode = = Infile_code) {
Mfilepath = Uri.decode (data.getdatastring ());
If the path obtained through Data.getdatastring () contains the Chinese path, garbled characters will appear, and the Uri.decode () function is decoded to get the correct path. But at this time the path is the URI path, must be converted to a string path, there are many methods on the net, I found by comparison, the URI path is more than file://string, so use the following method to intercept the string in front, get the string path, may not be good enough, The next step is to learn a better approach.
Mfilepath = mfilepath.substring (7, Mfilepath.length ());
}
}
This article is from the "Smile" blog, make sure to keep this source http://xmrs2014.blog.51cto.com/9528128/1574626
Android Open File Browser, select File to get back path