Android打开系统文件管理器
一、前言
Android 4.4(API級別19)引入了存儲訪問框架(SAF)。
通過SAF,用戶可以輕松地瀏覽和打開所有首選文檔存儲提供商中的文檔,圖像和其他文件。
也就是說,接下來介紹的方式適用于android4.4+的操作系統。
參考鏈接:https://developer.android.com/guide/topics/providers/document-provider
二、詳細實現
1、調出系統文件管理器界面
private static final int READ_REQUEST_CODE = 42; ... /*** Fires an intent to spin up the "file chooser" UI and select an image.*/ public void performFileSearch() {// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file// browser.Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);// Filter to only show results that can be "opened", such as a// file (as opposed to a list of contacts or timezones)intent.addCategory(Intent.CATEGORY_OPENABLE);// Filter to show only images, using the image MIME data type.// If one wanted to search for ogg vorbis files, the type would be "audio/ogg".// To search for all documents available via installed storage providers,// it would be "*/*".intent.setType("image/*");startActivityForResult(intent, READ_REQUEST_CODE); }1)Intent除了可以設置為ACTION_OPEN_DOCUMEN,還可以替換為ACTION_GET_CONTENT ,兩者的區別如下:
- 如果您希望您的應用僅讀取/導入數據,請使用ACTION_GET_CONTENT。 通過這種方法,應用程序可以導入數據的副本,例如圖像文件。
- 如果希望您的應用對文件提供者擁有的文件具有長期,持久的訪問權限,請使用ACTION_OPEN_DOCUMENT。
2)Category設置為CATEGORY_OPENABLE,過濾出可以打開的文件
3)Type設置的是MIMETYPE,比如:
- 過濾所有圖片,則設置為:"image/*"
- 只過濾png圖片,則設置為:"image/png"
- 過濾apk,那么可設置為:"application/vnd.android.package-archive"
4)startActivityForResult 啟動Inetent,以便在onActivityResult接收文件選擇結果
2、在onActivityResult中處理返回結果
@Override public void onActivityResult(int requestCode, int resultCode,Intent resultData) {// The ACTION_OPEN_DOCUMENT intent was sent with the request code// READ_REQUEST_CODE. If the request code seen here doesn't match, it's the// response to some other intent, and the code below shouldn't run at all.if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK) {// The document selected by the user won't be returned in the intent.// Instead, a URI to that document will be contained in the return intent// provided to this method as a parameter.// Pull that URI using resultData.getData().Uri uri = null;if (resultData != null) {uri = resultData.getData();Log.i(TAG, "Uri: " + uri.toString());showImage(uri);}} }1)從Intent中getData()獲取到返回的Uri對象,舊版本是file://開頭,新版本是content://開頭
3、從Uri獲取所需的信息
1)從Uri中獲取文件元數據,一般只能獲取到文件名與大小
public void dumpImageMetaData(Uri uri) {// The query, since it only applies to a single document, will only return// one row. There's no need to filter, sort, or select fields, since we want// all fields for one document.Cursor cursor = getActivity().getContentResolver().query(uri, null, null, null, null, null);try {// moveToFirst() returns false if the cursor has 0 rows. Very handy for// "if there's anything to look at, look at it" conditionals.if (cursor != null && cursor.moveToFirst()) {// Note it's called "Display Name". This is// provider-specific, and might not necessarily be the file name.String displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));Log.i(TAG, "Display Name: " + displayName);int sizeIndex = cursor.getColumnIndex(OpenableColumns.SIZE);// If the size is unknown, the value stored is null. But since an// int can't be null in Java, the behavior is implementation-specific,// which is just a fancy term for "unpredictable". So as// a rule, check if it's null before assigning to an int. This will// happen often: The storage API allows for remote files, whose// size might not be locally known.String size = null;if (!cursor.isNull(sizeIndex)) {// Technically the column stores an int, but cursor.getString()// will do the conversion automatically.size = cursor.getString(sizeIndex);} else {size = "Unknown";}Log.i(TAG, "Size: " + size);}} finally {cursor.close();} }2)將Uri轉換為Bitmap
private Bitmap getBitmapFromUri(Uri uri) throws IOException {ParcelFileDescriptor parcelFileDescriptor =getContentResolver().openFileDescriptor(uri, "r");FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();Bitmap image = BitmapFactory.decodeFileDescriptor(fileDescriptor);parcelFileDescriptor.close();return image; }3)將Uri轉換為InputStream
private String readTextFromUri(Uri uri) throws IOException {StringBuilder stringBuilder = new StringBuilder();try (InputStream inputStream =getContentResolver().openInputStream(uri);BufferedReader reader = new BufferedReader(new InputStreamReader(Objects.requireNonNull(inputStream)))) {String line;while ((line = reader.readLine()) != null) {stringBuilder.append(line);}}return stringBuilder.toString(); }MIMETYPE 常見的用法:
| 3gp | video/3gpp | pdb | chemical/x-pdb |
| aab | application/x-authoware-bin | application/pdf | |
| aam | application/x-authoware-map | pfr | application/font-tdpfr |
| aas | application/x-authoware-seg | pgm | image/x-portable-graymap |
| ai | application/postscript | pict | image/x-pict |
| aif | audio/x-aiff | pm | application/x-perl |
| aifc | audio/x-aiff | pmd | application/x-pmd |
| aiff | audio/x-aiff | png | image/png |
| als | audio/X-Alpha5 | pnm | image/x-portable-anymap |
| amc | application/x-mpeg | pnz | image/png |
| ani | application/octet-stream | pot | application/vnd.ms-powerpoint |
| apk | application/vnd.android.package-archive | ppm | image/x-portable-pixmap |
| asc | text/plain | pps | application/vnd.ms-powerpoint |
| asd | application/astound | ppt | application/vnd.ms-powerpoint |
| asf | video/x-ms-asf | pqf | application/x-cprplayer |
| asn | application/astound | pqi | application/cprplayer |
| asp | application/x-asap | prc | application/x-prc |
| asx | video/x-ms-asf | proxy | application/x-ns-proxy-autoconfig |
| au | audio/basic | ps | application/postscript |
| avb | application/octet-stream | ptlk | application/listenup |
| avi | video/x-msvideo | pub | application/x-mspublisher |
| awb | audio/amr-wb | pvx | video/x-pv-pvx |
| bcpio | application/x-bcpio | qcp | audio/vnd.qcelp |
| bin | application/octet-stream | qt | video/quicktime |
| bld | application/bld | qti | image/x-quicktime |
| bld2 | application/bld2 | qtif | image/x-quicktime |
| bmp | image/bmp | r3t | text/vnd.rn-realtext3d |
| bpk | application/octet-stream | ra | audio/x-pn-realaudio |
| bz2 | application/x-bzip2 | ram | audio/x-pn-realaudio |
| cal | image/x-cals | rar | application/x-rar-compressed |
| ccn | application/x-cnc | ras | image/x-cmu-raster |
| cco | application/x-cocoa | rdf | application/rdf+xml |
| cdf | application/x-netcdf | rf | image/vnd.rn-realflash |
| cgi | magnus-internal/cgi | rgb | image/x-rgb |
| chat | application/x-chat | rlf | application/x-richlink |
| class | application/octet-stream | rm | audio/x-pn-realaudio |
| clp | application/x-msclip | rmf | audio/x-rmf |
| cmx | application/x-cmx | rmm | audio/x-pn-realaudio |
| co | application/x-cult3d-object | rmvb | audio/x-pn-realaudio |
| cod | image/cis-cod | rnx | application/vnd.rn-realplayer |
| cpio | application/x-cpio | roff | application/x-troff |
| cpt | application/mac-compactpro | rp | image/vnd.rn-realpix |
| crd | application/x-mscardfile | rpm | audio/x-pn-realaudio-plugin |
| csh | application/x-csh | rt | text/vnd.rn-realtext |
| csm | chemical/x-csml | rte | x-lml/x-gps |
| csml | chemical/x-csml | rtf | application/rtf |
| css | text/css | rtg | application/metastream |
| cur | application/octet-stream | rtx | text/richtext |
| dcm | x-lml/x-evm | rv | video/vnd.rn-realvideo |
| dcr | application/x-director | rwc | application/x-rogerwilco |
| dcx | image/x-dcx | s3m | audio/x-mod |
| dhtml | text/html | s3z | audio/x-mod |
| dir | application/x-director | sca | application/x-supercard |
| dll | application/octet-stream | scd | application/x-msschedule |
| dmg | application/octet-stream | sdf | application/e-score |
| dms | application/octet-stream | sea | application/x-stuffit |
| doc | application/msword | sgm | text/x-sgml |
| dot | application/x-dot | sgml | text/x-sgml |
| dvi | application/x-dvi | sh | application/x-sh |
| dwf | drawing/x-dwf | shar | application/x-shar |
| dwg | application/x-autocad | shtml | magnus-internal/parsed-html |
| dxf | application/x-autocad | shw | application/presentations |
| dxr | application/x-director | si6 | image/si6 |
| ebk | application/x-expandedbook | si7 | image/vnd.stiwap.sis |
| emb | chemical/x-embl-dl-nucleotide | si9 | image/vnd.lgtwap.sis |
| embl | chemical/x-embl-dl-nucleotide | sis | application/vnd.symbian.install |
| eps | application/postscript | sit | application/x-stuffit |
| eri | image/x-eri | skd | application/x-Koan |
| es | audio/echospeech | skm | application/x-Koan |
| esl | audio/echospeech | skp | application/x-Koan |
| etc | application/x-earthtime | skt | application/x-Koan |
| etx | text/x-setext | slc | application/x-salsa |
| evm | x-lml/x-evm | smd | audio/x-smd |
| evy | application/x-envoy | smi | application/smil |
| exe | application/octet-stream | smil | application/smil |
| fh4 | image/x-freehand | smp | application/studiom |
| fh5 | image/x-freehand | smz | audio/x-smd |
| fhc | image/x-freehand | snd | audio/basic |
| fif | image/fif | spc | text/x-speech |
| fm | application/x-maker | spl | application/futuresplash |
| fpx | image/x-fpx | spr | application/x-sprite |
| fvi | video/isivideo | sprite | application/x-sprite |
| gau | chemical/x-gaussian-input | spt | application/x-spt |
| gca | application/x-gca-compressed | src | application/x-wais-source |
| gdb | x-lml/x-gdb | stk | application/hyperstudio |
| gif | image/gif | stm | audio/x-mod |
| gps | application/x-gps | sv4cpio | application/x-sv4cpio |
| gtar | application/x-gtar | sv4crc | application/x-sv4crc |
| gz | application/x-gzip | svf | image/vnd |
| hdf | application/x-hdf | svg | image/svg-xml |
| hdm | text/x-hdml | svh | image/svh |
| hdml | text/x-hdml | svr | x-world/x-svr |
| hlp | application/winhlp | swf | application/x-shockwave-flash |
| hqx | application/mac-binhex40 | swfl | application/x-shockwave-flash |
| htm | text/html | t | application/x-troff |
| html | text/html | tad | application/octet-stream |
| hts | text/html | talk | text/x-speech |
| ice | x-conference/x-cooltalk | tar | application/x-tar |
| ico | application/octet-stream | taz | application/x-tar |
| ief | image/ief | tbp | application/x-timbuktu |
| ifm | image/gif | tbt | application/x-timbuktu |
| ifs | image/ifs | tcl | application/x-tcl |
| imy | audio/melody | tex | application/x-tex |
| ins | application/x-NET-Install | texi | application/x-texinfo |
| ips | application/x-ipscript | texinfo | application/x-texinfo |
| ipx | application/x-ipix | tgz | application/x-tar |
| it | audio/x-mod | thm | application/vnd.eri.thm |
| itz | audio/x-mod | tif | image/tiff |
| ivr | i-world/i-vrml | tiff | image/tiff |
| j2k | image/j2k | tki | application/x-tkined |
| jad | text/vnd.sun.j2me.app-descriptor | tkined | application/x-tkined |
| jam | application/x-jam | toc | application/toc |
| jar | application/java-archive | toy | image/toy |
| jnlp | application/x-java-jnlp-file | tr | application/x-troff |
| jpe | image/jpeg | trk | x-lml/x-gps |
| jpeg | image/jpeg | trm | application/x-msterminal |
| jpg | image/jpeg | tsi | audio/tsplayer |
| jpz | image/jpeg | tsp | application/dsptype |
| js | application/x-javascript | tsv | text/tab-separated-values |
| jwc | application/jwc | tsv | text/tab-separated-values |
| kjx | application/x-kjx | ttf | application/octet-stream |
| lak | x-lml/x-lak | ttz | application/t-time |
| latex | application/x-latex | txt | text/plain |
| lcc | application/fastman | ult | audio/x-mod |
| lcl | application/x-digitalloca | ustar | application/x-ustar |
| lcr | application/x-digitalloca | uu | application/x-uuencode |
| lgh | application/lgh | uue | application/x-uuencode |
| lha | application/octet-stream | vcd | application/x-cdlink |
| lml | x-lml/x-lml | vcf | text/x-vcard |
| lmlpack | x-lml/x-lmlpack | vdo | video/vdo |
| lsf | video/x-ms-asf | vib | audio/vib |
| lsx | video/x-ms-asf | viv | video/vivo |
| lzh | application/x-lzh | vivo | video/vivo |
| m13 | application/x-msmediaview | vmd | application/vocaltec-media-desc |
| m14 | application/x-msmediaview | vmf | application/vocaltec-media-file |
| m15 | audio/x-mod | vmi | application/x-dreamcast-vms-info |
| m3u | audio/x-mpegurl | vms | application/x-dreamcast-vms |
| m3url | audio/x-mpegurl | vox | audio/voxware |
| ma1 | audio/ma1 | vqe | audio/x-twinvq-plugin |
| ma2 | audio/ma2 | vqf | audio/x-twinvq |
| ma3 | audio/ma3 | vql | audio/x-twinvq |
| ma5 | audio/ma5 | vre | x-world/x-vream |
| man | application/x-troff-man | vrml | x-world/x-vrml |
| map | magnus-internal/imagemap | vrt | x-world/x-vrt |
| mbd | application/mbedlet | vrw | x-world/x-vream |
| mct | application/x-mascot | vts | workbook/formulaone |
| mdb | application/x-msaccess | wav | audio/x-wav |
| mdz | audio/x-mod | wax | audio/x-ms-wax |
| me | application/x-troff-me | wbmp | image/vnd.wap.wbmp |
| mel | text/x-vmel | web | application/vnd.xara |
| mi | application/x-mif | wi | image/wavelet |
| mid | audio/midi | wis | application/x-InstallShield |
| midi | audio/midi | wm | video/x-ms-wm |
| mif | application/x-mif | wma | audio/x-ms-wma |
| mil | image/x-cals | wmd | application/x-ms-wmd |
| mio | audio/x-mio | wmf | application/x-msmetafile |
| mmf | application/x-skt-lbs | wml | text/vnd.wap.wml |
| mng | video/x-mng | wmlc | application/vnd.wap.wmlc |
| mny | application/x-msmoney | wmls | text/vnd.wap.wmlscript |
| moc | application/x-mocha | wmlsc | application/vnd.wap.wmlscriptc |
| mocha | application/x-mocha | wmlscript | text/vnd.wap.wmlscript |
| mod | audio/x-mod | wmv | audio/x-ms-wmv |
| mof | application/x-yumekara | wmx | video/x-ms-wmx |
| mol | chemical/x-mdl-molfile | wmz | application/x-ms-wmz |
| mop | chemical/x-mopac-input | wpng | image/x-up-wpng |
| mov | video/quicktime | wpt | x-lml/x-gps |
| movie | video/x-sgi-movie | wri | application/x-mswrite |
| mp2 | audio/x-mpeg | wrl | x-world/x-vrml |
| mp3 | audio/x-mpeg | wrz | x-world/x-vrml |
| mp4 | video/mp4 | ws | text/vnd.wap.wmlscript |
| mpc | application/vnd.mpohun.certificate | wsc | application/vnd.wap.wmlscriptc |
| mpe | video/mpeg | wv | video/wavelet |
| mpeg | video/mpeg | wvx | video/x-ms-wvx |
| mpg | video/mpeg | wxl | application/x-wxl |
| mpg4 | video/mp4 | x-gzip | application/x-gzip |
| mpga | audio/mpeg | xar | application/vnd.xara |
| mpn | application/vnd.mophun.application | xbm | image/x-xbitmap |
| mpp | application/vnd.ms-project | xdm | application/x-xdma |
| mps | application/x-mapserver | xdma | application/x-xdma |
| mrl | text/x-mrml | xdw | application/vnd.fujixerox.docuworks |
| mrm | application/x-mrm | xht | application/xhtml+xml |
| ms | application/x-troff-ms | xhtm | application/xhtml+xml |
| mts | application/metastream | xhtml | application/xhtml+xml |
| mtx | application/metastream | xla | application/vnd.ms-excel |
| mtz | application/metastream | xlc | application/vnd.ms-excel |
| mzv | application/metastream | xll | application/x-excel |
| nar | application/zip | xlm | application/vnd.ms-excel |
| nbmp | image/nbmp | xls | application/vnd.ms-excel |
| nc | application/x-netcdf | xlt | application/vnd.ms-excel |
| ndb | x-lml/x-ndb | xlw | application/vnd.ms-excel |
| ndwn | application/ndwn | xm | audio/x-mod |
| nif | application/x-nif | xml | text/xml |
| nmz | application/x-scream | xmz | audio/x-mod |
| nokia-op-logo | image/vnd.nok-oplogo-color | xpi | application/x-xpinstall |
| npx | application/x-netfpx | xpm | image/x-xpixmap |
| nsnd | audio/nsnd | xsit | text/xml |
| nva | application/x-neva1 | xsl | text/xml |
| oda | application/oda | xul | text/xul |
| oom | application/x-AtlasMate-Plugin | xwd | image/x-xwindowdump |
| pac | audio/x-pac | xyz | chemical/x-pdb |
| pae | audio/x-epac | yz1 | application/x-yz1 |
| pan | application/x-pan | z | application/x-compress |
| pbm | image/x-portable-bitmap | zac | application/x-zaurus-zac |
| pcx | image/x-pcx | zip | application/zip |
三、文件創建、編輯與刪除
1、創建文件,可以在onActivity中接收到結果
// Here are some examples of how you might call this method. // The first parameter is the MIME type, and the second parameter is the name // of the file you are creating: // // createFile("text/plain", "foobar.txt"); // createFile("image/png", "mypicture.png");// Unique request code. private static final int WRITE_REQUEST_CODE = 43; ... private void createFile(String mimeType, String fileName) {Intent intent = new Intent(Intent.ACTION_CREATE_DOCUMENT);// Filter to only show results that can be "opened", such as// a file (as opposed to a list of contacts or timezones).intent.addCategory(Intent.CATEGORY_OPENABLE);// Create a file with the requested MIME type.intent.setType(mimeType);intent.putExtra(Intent.EXTRA_TITLE, fileName);startActivityForResult(intent, WRITE_REQUEST_CODE); }2、獲取Uri之后,要是文件的Document.COLUMN_FLAGS 包含SUPPORTS_DELETE,則可以刪除該文件:
DocumentsContract.deleteDocument(getContentResolver(), uri);3、編輯文件
private static final int EDIT_REQUEST_CODE = 44; /*** Open a file for writing and append some text to it.*/private void editDocument() {// ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's// file browser.Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);// Filter to only show results that can be "opened", such as a// file (as opposed to a list of contacts or timezones).intent.addCategory(Intent.CATEGORY_OPENABLE);// Filter to show only text files.intent.setType("text/plain");startActivityForResult(intent, EDIT_REQUEST_CODE); }private void alterDocument(Uri uri) {try {ParcelFileDescriptor pfd = getActivity().getContentResolver().openFileDescriptor(uri, "w");FileOutputStream fileOutputStream =new FileOutputStream(pfd.getFileDescriptor());fileOutputStream.write(("Overwritten by MyCloud at " +System.currentTimeMillis() + "\n").getBytes());// Let the document provider know you're done by closing the stream.fileOutputStream.close();pfd.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();} }4、檢查最新的文件數據
final int takeFlags = intent.getFlags()& (Intent.FLAG_GRANT_READ_URI_PERMISSION| Intent.FLAG_GRANT_WRITE_URI_PERMISSION); // Check for the freshest data. getContentResolver().takePersistableUriPermission(uri, takeFlags);四、拓展
1、Android 7.0在存儲訪問框架中添加了虛擬文件的概念。
2、即使虛擬文件沒有二進制表示形式,您的客戶端應用程序也可以通過將其強制轉換為其他文件類型或使用ACTION_VIEW意圖查看這些文件來打開其內容。
3、要打開虛擬文件,您的客戶端應用程序需要包含特殊的邏輯來處理它們。如果要獲取文件的字節表示形式(例如,預覽文件),則需要從文檔提供者處請求其他MIME類型。
4、要在您的應用程序中獲取虛擬文檔的URI,首先要創建一個Intent以打開文件選擇器UI,就像前面在Seach中為文檔顯示的代碼一樣。
5、重要提示:由于應用無法使用openInputStream()方法直接打開虛擬文件,因此,如果您在ACTION_OPEN_DOCUMENT意向中包括CATEGORY_OPENABLE類別,則您的應用將不會收到任何虛擬文件。用戶做出選擇后,系統將調用onActivityResult()方法,如先前在處理結果中所示。您的應用可以檢索文件的URI,然后使用類似于以下代碼片段的方法來確定文件是否為虛擬文件。驗證文件為虛擬文件后,可以將其強制轉換為其他MIME類型,例如圖像文件。以下代碼段顯示了如何檢查虛擬文件是否可以表示為映像,如果可以,則從虛擬文件獲取輸入流。
總結
以上是生活随笔為你收集整理的Android打开系统文件管理器的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: project 模板_施工进度横道图不会
- 下一篇: PyOpenCL图像处理:Box模糊