Android Open File Browser, select File to get back path

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.