Android calls the built-in File Manager to select files.

Source: Internet
Author: User

Http://blog.csdn.net/zqchn/article/details/8770913

The project network disk that is being created these days. When uploading files, you need to call the File Manager that comes with the system to select files. Later, you are considering how to call the files. You have searched the internet for a long time and there is no good solution, later, I found a good article and used the code for reference.

/** Select software to call the file to select the file **/private void showfilechooser () {intent = new intent (intent. action_get_content); intent. settype ("*/*"); intent. addcategory (intent. category_openable); try {startactivityforresult (intent. createchooser (intent, "select a file to upload"), file_select_code);} catch (Android. content. activitynotfoundexception ex) {// potentially direct the user to the market with a dialogtoast. maketext (getactivity (), "Please install file manager", toast. length_short ). show ();}}

In catch, we can do more operations, such as redirecting to a page for downloading the File Manager or so on.

How to handle the returned data. Upload in my project is received as follows

/** Upload operations based on the selected returned file **/@ overridepublic void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubif (resultcode = activity. result_ OK) {// get the URI of the selected fileuri uri = data. getdata (); string URL; try {url = ffileutils. getpath (getactivity (), Uri); log. I ("ht", "url" + URL); string filename = URL. substring (URL. lastindexof ("/") + 1); intent = new intent (getactivity (), uploadservices. class); intent. putextra ("FILENAME", filename); intent. putextra ("url", URL); intent. putextra ("type", ""); intent. putextra ("fuid", ""); intent. putextra ("type", ""); getactivity (). startservice (intent);} catch (urisyntaxexception e) {// todo auto-generated catch blocke. printstacktrace () ;}} super. onactivityresult (requestcode, resultcode, data );}

 

 

---------------------------------------------------------

Private Static final int file_select_code = 0; Private Static final string tag = "videoactivity"; private void choosefile () {intent = new intent (intent. action_get_content); intent. settype ("*/*"); intent. addcategory (intent. category_openable); try {startactivityforresult (intent. createchooser (intent, "select file"), file_select_code);} catch (Android. content. activitynotfoundexception ex) {toast. maketext (Th Is, "You have a file manager -_-!! ", Toast. length_short). Show () ;}@ overridepublic void onactivityresult (INT requestcode, int resultcode, intent data) {// todo auto-generated method stubif (resultcode! = Activity. result_ OK) {log. E (TAG, "onactivityresult () error, resultcode:" + resultcode); super. onactivityresult (requestcode, resultcode, data); return;} If (requestcode = file_select_code) {URI uri = data. getdata (); log. I (TAG, "------->" + Uri. getpath ();} super. onactivityresult (requestcode, resultcode, data );}

 

 

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.