Android calls the File Manager and returns the path of the selected file. android File Manager
In actual projects, you often need to call the File Manager to select the download path or the local file path to upload. Today we will give you a demo to demonstrate the implementation process of this function.
I. Implementation Effect preview the following is the testing result of the Samsung S6 prototype. Of course, the effects of different phone calls are different. 2. The layout file for code implementation is very simple. There is a Button and TextView. We will not demonstrate it here. We will directly implement the Code:
1 package com.panhouye.selectfile;
2
3 import android.annotation.SuppressLint;
4 import android.app.Activity;
5 import android.content.ContentUris;
6 import android.content.Context;
7 import android.content.Intent;
8 import android.database.Cursor;
9 import android.net.Uri;
10 import android.os.Build;
11 import android.os.Bundle;
12 import android.os.Environment;
13 import android.provider.DocumentsContract;
14 import android.provider.MediaStore;
15 import android.support.v7.app.AppCompatActivity;
16 import android.view.View;
17 import android.widget.Button;
18 import android.widget.TextView;
19 import android.widget.Toast;
20
21 public class MainActivity extends AppCompatActivity {
22 TextView tv;
23 @Override
24 protected void onCreate (Bundle savedInstanceState) {
25 super.onCreate (savedInstanceState);
26 setContentView (R.layout.activity_main);
27 Button btn = (Button) findViewById (R.id.btn);
28 tv = (TextView) findViewById (R.id.tv);
29 btn.setOnClickListener (new View.OnClickListener () {
30 @Override
31 public void onClick (View v) {
32 Intent intent = new Intent (Intent.ACTION_GET_CONTENT);
33 //intent.setType(“image/*”);//Select image
34 //intent.setType(“audio/* ”); // Select audio
35 //intent.setType("video/* "); // Select video
36 //intent.setType(“video/*;image/*”);//Select video and picture at the same time
37 intent.setType ("* / *"); // No type restriction
38 intent.addCategory (Intent.CATEGORY_OPENABLE);
39 startActivityForResult (intent, 1);
40}
41});
42}
43 String path;
44 @Override
45 protected void onActivityResult (int requestCode, int resultCode, Intent data) {
46 if (resultCode == Activity.RESULT_OK) {
47 Uri uri = data.getData ();
48 if ("file" .equalsIgnoreCase (uri.getScheme ()))
49 path = uri.getPath ();
50 tv.setText (path);
51 Toast.makeText (this, path + "11111", Toast.LENGTH_SHORT) .show ();
52 return;
53}
54 if (Build.VERSION.SDK_INT> Build.VERSION_CODES.KITKAT) {//4.4 and later
55 path = getPath (this, uri);
56 tv.setText (path);
57 Toast.makeText (this, path, Toast.LENGTH_SHORT) .show ();
58} else {//4.4 system call method below
59 path = getRealPathFromURI (uri);
60 tv.setText (path);
61 Toast.makeText (MainActivity.this, path + "222222", Toast.LENGTH_SHORT) .show ();
62}
63}
64}
65
66 public String getRealPathFromURI (Uri contentUri) {
67 String res = null;
68 String [] proj = {MediaStore.Images.Media.DATA};
69 Cursor cursor = getContentResolver (). Query (contentUri, proj, null, null, null);
70 if (null! = Cursor && cursor.moveToFirst ()) {;
71 int column_index = cursor.getColumnIndexOrThrow (MediaStore.Images.Media.DATA);
72 res = cursor.getString (column_index);
73 cursor.close ();
74}
75 return res;
76}
77
78 / **
79 * Obtain the absolute path of the file from Uri specially designed for Android4.4, the previous method is not good enough
80 * /
81 @SuppressLint ("NewApi")
82 public String getPath (final Context context, final Uri uri) {
83
84 final boolean isKitKat = Build.VERSION.SDK_INT> = Build.VERSION_CODES.KITKAT;
85
86 // DocumentProvider
87 if (isKitKat && DocumentsContract.isDocumentUri (context, uri)) {
88 // ExternalStorageProvider
89 if (isExternalStorageDocument (uri)) {
90 final String docId = DocumentsContract.getDocumentId (uri);
91 final String [] split = docId.split (":");
92 final String type = split [0];
93
94 if ("primary" .equalsIgnoreCase (type)) {
95 return Environment.getExternalStorageDirectory () + "/" + split [1];
96}
97}
98 // DownloadsProvider
99 else if (isDownloadsDocument (uri)) {
100
101 final String id = DocumentsContract.getDocumentId (uri);
102 final Uri contentUri = ContentUris.withAppendedId (
103 Uri.parse ("content: // downloads / public_downloads"), Long.valueOf (id));
104
105 return getDataColumn (context, contentUri, null, null);
106}
107 // MediaProvider
108 else if (isMediaDocument (uri)) {
109 final String docId = DocumentsContract.getDocumentId (uri);
110 final String [] split = docId.split (":");
111 final String type = split [0];
112
113 Uri contentUri = null;
114 if ("image" .equals (type)) {
115 contentUri = MediaStore.Images.Media.EX
TERNAL_CONTENT_URI;
116} else if ("video" .equals (type)) {
117 contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
118} else if ("audio" .equals (type)) {
119 contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
120}
121
122 final String selection = "_id =?";
123 final String [] selectionArgs = new String [] {split [1]};
124
125 return getDataColumn (context, contentUri, selection, selectionArgs);
126}
127}
128 // MediaStore (and general)
129 else if ("content" .equalsIgnoreCase (uri.getScheme ())) {
130 return getDataColumn (context, uri, null, null);
131}
132 // File
133 else if ("file" .equalsIgnoreCase (uri.getScheme ())) {
134 return uri.getPath ();
135}
136 return null;
137}
138
139 / **
140 * Get the value of the data column for this Uri. This is useful for
141 * MediaStore Uris, and other file-based ContentProviders.
142 *
143 * @param context The context.
144 * @param uri The Uri to query.
145 * @param selection (Optional) Filter used in the query.
146 * @param selectionArgs (Optional) Selection arguments used in the query.
147 * @return The value of the _data column, which is typically a file path.
148 * /
149 public String getDataColumn (Context context, Uri uri, String selection,
150 String [] selectionArgs) {
151
152 Cursor cursor = null;
153 final String column = "_data";
154 final String [] projection = {column};
155
156 try {
157 cursor = context.getContentResolver (). Query (uri, projection, selection, selectionArgs,
158 null);
159 if (cursor! = Null && cursor.moveToFirst ()) {
160 final int column_index = cursor.getColumnIndexOrThrow (column);
161 return cursor.getString (column_index);
162}
163} finally {
164 if (cursor! = Null)
165 cursor.close ();
166}
167 return null;
168}
169
170 / **
171 * @param uri The Uri to check.
172 * @return Whether the Uri authority is ExternalStorageProvider.
173 * /
174 public boolean isExternalStorageDocument (Uri uri) {
175 return "com.android.externalstorage.documents" .equals (uri.getAuthority ());
176}
177
178 / **
179 * @param uri The Uri to check.
180 * @return Whether the Uri authority is DownloadsProvider.
181 * /
182 public boolean isDownloadsDocument (Uri uri) {
183 return "com.android.providers.downloads.documents" .equals (uri.getAuthority ());
184}
185
186 / **
187 * @param uri The Uri to check.
188 * @return Whether the Uri authority is MediaProvider.
189 * /
190 public boolean isMediaDocument (Uri uri) {
191 return "com.android.providers.media.documents" .equals (uri.getAuthority ());
192}
193}
Iii. Summary This is the file management that comes with the calling system. It cannot guarantee and control the unified interface display. Therefore, we still need to implement a unified file management interface with our own apps.