Android calls the File Manager that comes with the system to select a file and obtain the path. android File Manager

Source: Internet
Author: User

(Conversion) Android calls the built-in File Manager to select files and obtain the path. android File Manager

Unlike the sandbox mode of iOS, Android allows you to browse local memory in a file browser. Android APIS also provide corresponding interfaces.

The basic idea is to first use the Android API to call the file browser that comes with the system to select a file to obtain the URI, and then convert the URI to file to get the file.

Call the file browser that comes with the System
Public class MainActivity extends AppCompatActivity {@ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); Button btn = (Button) findViewById (R. id. btn); btn. setOnClickListener (new View. onClickListener () {@ Override public void onClick (View v) {Intent intent = new Intent (Intent. ACTION_GET_CONTENT); intent. setType (" */* "); // Set the type. Here I am using any type. Any suffix can be written like this. Intent. addCategory (Intent. CATEGORY_OPENABLE); startActivityForResult (intent, 1 );}});}}
Intent. setType ("image/*"); // intent. setType ("audio/*"); // select audio // intent. setType ("video/*"); // select the video (mp4 3gp is the video format supported by android) // intent. setType ("video/*; image/*"); // select both the video and image
Callback
@ Overrideprotected void onActivityResult (int requestCode, int resultCode, Intent data) {if (resultCode = Activity. RESULT_ OK) {// whether to select the Uri uri = data. getData (); // get the uri, followed by the process of converting the uri into a file. String [] proj = {MediaStore. images. media. DATA}; Cursor actualimagecursor = managedQuery (uri, proj, null); int actual_image_column_index = actualimagecursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); actualimagecursor. moveToFirst (); String img_path = actualimagecursor. getString (actual_image_column_index); File file = new File (img_path); Toast. makeText (MainActivity. this, file. toString (), Toast. LENGTH_SHORT ). show ();}}

The onActivityResult function responds to the operations on the selected file.

Reprint to: http://www.banbaise.com/archives/614

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.