android+选择目录,Android的目录选择库

Android DirectoryChooser

687474703a2f2f696d672e736869656c64732e696f2f7472617669732f70617373792f416e64726f69642d4469726563746f727943686f6f7365722e7376673f7374796c653d666c6174

687474703a2f2f696d672e736869656c64732e696f2f6769747469702f70617373792e7376673f7374796c653d666c6174

68747470733a2f2f696d672e736869656c64732e696f2f6d6176656e2d63656e7472616c2f762f6e65742e72647265692e616e64726f69642e64697263686f6f7365722f6c6962726172792e737667

68747470733a2f2f62616467652e776166666c652e696f2f70617373792f616e64726f69642d6469726563746f727963686f6f7365722e7376673f6c6162656c3d7265616479267469746c653d5265616479

68747470733a2f2f696d672e736869656c64732e696f2f62616467652f416e64726f6964253230417273656e616c2d4469726563746f727943686f6f7365722d626c75652e7376673f7374796c653d666c6174

A simple directory chooser you can integrate into your Android app.

This version of the library has no additional dependencies, but requires Android v11+ to work.there is, however, a pre-v11-branch that supports down to v7 using ActionBarSherlock.

You can download the sample app from the Play Store :

4cfface388dd757843cae59b4fe4b9d7.png

Based on the DirectoryChooser from the excellent AntennaPod App by danieloeh.As stand-alone Activity

chooser-activity.pngas DialogFragment

chooser-dialog.png

Usage

For a full example see the sample app in the repository.

from Maven Central

Library releases are available on Maven Central, snapshots can be retrieved from Sonatype :

Release (SDK 11 +)

Gradlecompile 'net.rdrei.android.dirchooser:library:3.2@aar'

Maven

net.rdrei.android.dirchooser

library

3.2

aar

Release (SDK 7 +)

Gradlecompile 'net.rdrei.android.dirchooser:library:1.0-pre-v11@aar'

Maven

net.rdrei.android.dirchooser

library

1.0-pre-v11

aar

Snapshot (SDK 11 +)compile 'net.rdrei.android.dirchooser:library:3.2-SNAPSHOT@aar'

Snapshot (SDK 4 +)compile 'net.rdrei.android.dirchooser:library:2.0-pre-v11-SNAPSHOT@aar'

as Library Project

Alternatively, check out this repository and add it as a library project.$ git clone https://github.com/passy/Android-DirectoryChooser.git

Import the project into your favorite IDE and addandroid.library.reference.1=/path/to/Android-DirectoryChooser/libraryto your project.properties.

Manifest

You need to declare the DirectoryChooserActivity and request theandroid.permission.WRITE_EXTERNAL_STORAGEpermission.

...

Activity

To choose a directory, start the activity from your app logic :finalIntent chooserIntent =newIntent(this, DirectoryChooserActivity.class);finalDirectoryChooserConfig config =DirectoryChooserConfig.builder()

. newDirectoryName("DirChooserSample")

. allowReadOnlyDirectory(true)

. allowNewDirectoryNameModification(true)

. build();

chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);// REQUEST_DIRECTORY is a constant integer to identify the request, e.g. 0startActivityForResult(chooserIntent, REQUEST_DIRECTORY);

Handle the result in your onActivityResult method :@Overrideprotectedvoid onActivityResult(int requestCode, int resultCode, Intent data) {

super.onActivityResult(requestCode, resultCode, data);

if (requestCode ==REQUEST_DIRECTORY) {

if (resultCode ==DirectoryChooserActivity.RESULT_CODE_DIR_SELECTED) {

handleDirectoryChoice(data

. getStringExtra(DirectoryChooserActivity.RESULT_SELECTED_DIR));

} else {

// Nothing selected }

}

}

Fragment

You can also use the underlying DialogFragment for even better integration.whether you use the Fragment as a Dialog or not is completely up to you.all you have to do is implement the OnFragmentInteractionListener interface to respond to the events that a directory is selected or the user cancels the dialog :publicclassDirChooserFragmentSampleextendsActivityimplementsDirectoryChooserFragment.OnFragmentInteractionListener {

privateTextView mDirectoryTextView;

privateDirectoryChooserFragment mDialog;

@OverrideprotectedvoidonCreate(BundlesavedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.dialog);

finalDirectoryChooserConfig config =DirectoryChooserConfig.builder()

. newDirectoryName("DialogSample")

. build();

mDialog =DirectoryChooserFragment.newInstance(config);

mDirectoryTextView = (TextView) findViewById(R.id.textDirectory);

findViewById(R.id.btnChoose)

. setOnClickListener(newView.OnClickListener() {

@OverridepublicvoidonClick(Viewv) {

mDialog.show(getFragmentManager(), null);

}

});

}

@OverridepublicvoidonSelectDirectory(@NonNullStringpath) {

mDirectoryTextView.setText(path);

mDialog.dismiss();

}

@OverridepublicvoidonCancelChooser() {

mDialog.dismiss();

}

}

If calling the directory chooser from your own fragment, be sure to set the target fragment first :@Overridepublicvoid onClick(View v) {

mDialog.setTargetFragment(this, 0);

mDialog.show(getFragmentManager(), null);

}

Configuration

The Directory Chooser is configured through a parcelable configuration object, which is great because it means that you don't have to tear your hair out over finicky string extras.Instead, you get auto-completion and a nice immutable data structure.here's what you can configure :newDirectoryName : String (required )

Name of the directory to create.User can change this name when he creates the folder.to avoid this use allowNewDirectoryNameModification argument.initialDirectory : String (default :"")

Optional argument to define the path of the directory that will be shown first.if it is not sent or if path denotes a non readable/writable directory or it is not a directory, it defaults toandroid.os.Environment#getExternalStorageDirectory()allowReadOnlyDirectory : Boolean (default :false )

Argument to define whether or not the directory chooser allows read-only paths to be chosen.if it false only directories with read-write access can be chosen.allowNewDirectoryNameModification : Boolean (default :false )

Argument to define whether or not the directory chooser allows modification of provided new directory name.examplefinalDirectoryChooserConfig config =DirectoryChooserConfig.builder()

. newDirectoryName("DialogSample")

. allowNewDirectoryNameModification(true)

. allowReadOnlyDirectory(true)

. initialDirectory("/sdcard")

. build();

Apps using thispopcorntime.pngPopcorn Time for Androidemby.pngEmby for Androidscdl.pngDownloader for SoundCloudwallpaperchanger.pngWallpaper Changerxnretro.pngXnRetroinstasave.pngInstaSave

mlmanager.pngML Managerautoguard.pngAutoGuardCompanionForBand.pngCompanion for Band

To add your own app, please send a pull request.

Releasing

Preparation

To release a new snapshot on Maven Central, add your credentials to ~/.gradle/gradle.properties (you get them from http://oss.sonatype.org ) as well as your signing GPG key :signing.keyId=0x18EEA4AF

signing.secretKeyRingFile=/home/pascal/.gnupg/secring.gpg

NEXUS_USERNAME=username

NEXUS_PASSWORD=password

Staging

To upload a new snapshot, just run gradle's uploadArchives command :gradle :library:uploadArchives

Release

Update versions and remove -SNAPSHOT suffixes.gradle build :library:uploadArchives

LicenseCopyright 2013-2016 Pascal Hartig

Licensed under the Apache License, Version 2.0 (the"License");

you may not use this file except in compliance with the License.

You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software

distributed under the License is distributed on an"AS IS" BASIS,

WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.

See the License for the specific language governing permissions and

limitations under the License.

Thanks

Sample App icon by Frank Souza.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值