(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