Android:快速选择文件夹或指定多个扩展名的文件

2015年08月17日 Android 8条评论 阅读8147次

近期经常会遇到一个选择Android手机上存储中某个文件夹的问题。

本文发的是一个自定义的对话框,实现的功能就是弹出手机上所有文件夹/指定扩展名的文件,按下确定后,可以返回用户选择的文件/文件夹的路径。

先看效果图:

Image

代码很简单的。基本就是继承Dialog类,在增加一个回调事件来返回文件/文件夹的路径地址。

因为要考虑到用户是想要选择文件夹还是指定某个扩展名的文件或是所有类型的文件,所以在Dialog的构造方法中使用了不定长参数。

public FolderFilePicker(Context context, PickPathEvent event, String... extensions)


使用过程中会有下面几种情况:

  • 若只想选择文件夹,则只需要传递前两个参数即可FolderFilePicker picker = new FolderFilePicker(context, event);
  • 若想选择指定扩展名的文件,则在第三个参数中写出即可FolderFilePicker picker = new FolderFilePicker(context, event, "jpg");
  • 若想选择多个扩展名的文件,则在第三个参数中继续写出即可FolderFilePicker picker = new FolderFilePicker(context, event, "jpg", "png");
  • 若想选择任意扩展名的文件,则在第三个参数中指定为*即可FolderFilePicker picker = new FolderFilePicker(context, event, "*")、


这是构造方法的第三个参数的说明,另外第二个参数是我自定义的一个回调,用来接收选择的具体文件/文件夹路径。

示例:

new PickPathEvent() {
    @Override
    public void onPickEvent(String resultPath) {
     Toast.makeText(MainActivity.this, resultPath, Toast.LENGTH_LONG).show();
    }
}

其中resultPath就是对话框返回的路径字符串。

最后,调用picker.show()就可以使其显示使用了。


遇到的问题


主要说一下对话框的标题栏显示不全标题的问题。

标题栏用来显示当前所在路径的作用,当点击了一个文件夹进去之后,标题栏则会更新为当前路径,当返回到上一层时,同样也会更新。

在测试中发现,当现在所处的路径比较深时,对话框的标题栏就会显示不全,只显示前面一部分。

因为没找到扩展标准对话框中标题栏的设置,所以在自定义对话框中,把标题栏去掉,在布局文件中顶部加了一个TextView充当标题栏。

此时就可以通过对TextView设置属性来使其跑马灯显示了。

增加了下面三行代码:

android:ellipsize="marquee"
android:focusable="true"
android:focusableInTouchMode="true"


请特别注意最后两行代码不要忘记添加,返回跑马灯会跑不起来的。

最后,还要说明的是,AndroidManifest.xml中不要忘记添加如下权限申请,否则,你的list中将会是空的,看不到任何文件。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


最后,我把这个作为了libary工程放到了gitbub上,有需要的可以参考下,有问题或好的建议麻烦指出。多谢!

github地址:https://github.com/will86/folderfilepicker

分享本文至:

WRITTEN BY

avatar
本文标签:githubdialog
看了本文是不是觉得很赞,那就赶紧点击下面按钮分享给身边的朋友吧!

8 条评论

  1. avatar bcwatershow

    <a href="http://www.aquivlar.com/air-jordan-retro-5-ni%C3%B1os-rosado-gris-scarped">air jordan retro 5 ni帽os rosado gris</a> <a href="http://www.brbuyeragent.com/nike-air-max-90-ice-qs-nero-cool-grigio-kurpesc">nike air max 90 ice qs nero cool grigio</a> <a href="http://www.eskortka.com/nike-rockets-13-james-harden-camo-nba-swingman-realtree-collection-jersey-nflr">nike rockets 13 james harden camo nba swingman realtree collection jersey</a> <a href="http://www.gypsandco.com/air-jordan-retro-1-dam%C3%A4nner-rosa-gr%C3%BCn-butyg">air jordan retro 1 dam盲nner rosa gr眉n</a> <a href="http://www.hoakimcham.com/youth-nike-los-angeles-rams-89-tyler-higbee-limited-gold-rush-vapor-untouchable-nfl-jersey-nflz">youth nike los angeles rams 89 tyler higbee limited gold rush vapor untouchable nfl jersey</a> <a href="http://www.spiritkidsedu.com/limited-nike-brown-mens-david-njoku-jersey-nfl-85-cleveland-browns-therma-long-sleeve-nflx">limited nike brown mens david njoku jersey nfl 85 cleveland browns therma long sleeve</a>
    bcwatershow http://www.bcwatershow.com/

  2. avatar donggeomdo

    <a href="http://www.khalataxi.com/nike-air-max-2017-kpu-ii-svart-s%C3%B8lv-chaussuresa">nike air max 2017 kpu ii svart s酶lv</a> <a href="http://www.miaclearbras.com/nike-new-england-patriots-70-logan-mankins-elite-navy-blue-team-color-jersey-nflc">nike new england patriots 70 logan mankins elite navy blue team color jersey</a> <a href="http://www.divyangbazar.com/black-and-white-air-max-1-runningg">black and white air max 1</a> <a href="http://www.dixiepundit.com/zdeno-chara-33-boston-bruins-reebok-premier-alternate-jersey-small-nflv">zdeno chara 33 boston bruins reebok premier alternate jersey small</a> <a href="http://www.ifshaildamage.com/nike-lebron-witness-grey-orange-trainersa">nike lebron witness grey orange</a> <a href="http://www.nanterrelec.com/nike-free-5.0-svart-and-s%C3%B8lv-scarpec">nike free 5.0 svart and s酶lv</a>
    donggeomdo http://www.donggeomdo.com/

  3. avatar inenigroup

    <a href="http://www.tryautolikes.com/pink-nike-mercurial-football-boots-skox">pink nike mercurial football boots</a> <a href="http://www.bigbigk.com/silver-orange-womens-nike-kobe-5-shoes-sportsd">silver orange womens nike kobe 5 shoes</a> <a href="http://www.gmanswindle.com/coach-open-toe-shoes-coachr">coach open toe shoes</a> <a href="http://www.ambitionagent.com/limited-mark-herzlich-jersey-new-york-giants-94-home-royal-blue-nfld">limited mark herzlich jersey new york giants 94 home royal blue</a> <a href="http://www.cheryberber.com/nike-free-7-womens-gold-silver-sportsb">nike free 7 womens gold silver</a> <a href="http://www.wlstoday.com/bon-march%C3%A9-nike-flex-fury-femmes-gris-trainersf">bon march茅 nike flex fury femmes gris</a>
    inenigroup http://www.inenigroup.com/

  4. avatar daddeaze

    <a href="http://www.hotelfuat.com/all-black-converse-high-tops-womens-shoesz">all black converse high tops womens</a> <a href="http://www.pmisaltriver.com/jordan-retro-7-bl%C3%A5-sort-schuheb">jordan retro 7 bl氓 sort</a> <a href="http://www.ductambag.com/benardrick-mckinney-ash-backer-nfl-nike-houston-texans-55-pullover-hoodie-nflu">benardrick mckinney ash backer nfl nike houston texans 55 pullover hoodie</a> <a href="http://www.priverussia.com/gold-new-balance-996-skou">gold new balance 996</a> <a href="http://www.coatsfrance.com/nike-free-1.0-womens-silver-white-runningf">nike free 1.0 womens silver white</a> <a href="http://www.intoloo.com/womens-white-adidas-trainers-zapatosu">womens white adidas trainers</a>
    daddeaze http://www.daddeaze.com/

  5. avatar bettytatar

    <a href="http://www.tickbits.com/nike-air-max-zero-dam%C3%A4nner-wei%C3%9F-gelb-scarpee">nike air max zero dam盲nner wei脽 gelb</a> <a href="http://www.udsgamebrazil.com/air-jordan-melo-m11-argent-orange-obuvc">air jordan melo m11 argent orange</a> <a href="http://www.ebinsaat.com/air-jordan-31-todas-star-coins-scarpea">air jordan 31 todas star coins</a> <a href="http://www.gayburner.com/nike-lunarglide-6-mennns-l%C3%B8ping-sneaker-chaussuresg">nike lunarglide 6 mennns l酶ping sneaker</a> <a href="http://www.gaydaytrips.com/nike-kobe-9-low-em-blanc-or-unblocked-kurpese">nike kobe 9 low em blanc or unblocked</a> <a href="http://www.jmgtours.com/adidas-prougeator-absolion-blanc-rouge-obuva">adidas prougeator absolion blanc rouge</a>
    bettytatar http://www.bettytatar.com/

  6. avatar 2ihz9gpj

    <a href="http://www.brng84c3.com/nike-lebron-barn-oransje-gr%C3%A5-butyf">nike lebron barn oransje gr氓</a> <a href="http://www.svdnoaq1.com/nike-air-jordan-13-retro-hommes-basketbtout-chaussure-pattern-scarpec">nike air jordan 13 retro hommes basketbtout chaussure pattern</a> <a href="http://www.t1vyycje.com/adidas-stan-smith-zebra-print-kondisko-chaussuresf">adidas stan smith zebra print kondisko</a> <a href="http://www.vcjmgh9f.com/nike-foamposite-one-gold-rot-butya">nike foamposite one gold rot</a> <a href="http://www.x6dbypaf.com/nike-kd-9-elite-rose-dust-lipstick-schuhea">nike kd 9 elite rose dust lipstick</a> <a href="http://www.a5ckyuq3.com/nike-hyperrev-2017-bl%C3%A5-orange-schuhec">nike hyperrev 2017 bl氓 orange</a>
    [url=http://www.2ihz9gpj.com/]2ihz9gpj[/url]

  7. avatar k3htroya

    <a href="http://www.1soiyu28.com/nike-air-force-1-flyknit-low-hvid-queen-kurpesc">nike air force 1 flyknit low hvid queen</a> <a href="http://www.cx77f2er.com/air-jordan-3-88-svart-cemennt-2014-obuva">air jordan 3 88 svart cemennt 2014</a> <a href="http://www.7x8rq331.com/jordan-fusion-8-jaune-schuhef">jordan fusion 8 jaune</a> <a href="http://www.it6zdy3w.com/nike-free-run-2-dam%C3%A4nner-schwarz-and-blau-chaussuresg">nike free run 2 dam盲nner schwarz and blau</a> <a href="http://www.ybavsh19.com/asics-gel-quantum-360-lila-braun-obuvd">asics gel quantum 360 lila braun</a> <a href="http://www.2n946caj.com/jordan-10-powder-bl%C3%A5-hype-kurpesc">jordan 10 powder bl氓 hype</a>
    [url=http://www.k3htroya.com/]k3htroya[/url]

  8. avatar oktoberfestmn

    <a href="http://www.momandpopop.com/elite-spencer-long-washington-redskins-road-jersey-61-white-nflb">elite spencer long washington redskins road jersey 61 white</a> <a href="http://www.artlaurey.com/nike-zoom-pegasus-32-womens-grey-paint-skod">nike zoom pegasus 32 womens grey paint</a> <a href="http://www.ochapan.com/burberry-scarf-shop-scarfr">burberry scarf shop</a> <a href="http://www.brooklyngay.com/air-jordan-after-game-herren-orange-sneakera">air jordan after game herren orange</a> <a href="http://www.royphelps.com/billig-nike-free-l%C3%B8ping-sko-kvinners-oransje-shoesb">billig nike free l酶ping sko kvinners oransje</a> <a href="http://www.agneaublanc.com/hugo-boss-mens-tie-tiea">hugo boss mens tie</a>
    oktoberfestmn http://www.oktoberfestmn.com/

欢迎留言




用户登录

sitemap