Skip to content

Android library that provides a file explorer and allows users to select files on external storage.

License

Notifications You must be signed in to change notification settings

tryprasannan/aFileChooser

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

9 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

aFileChooser - Android File Chooser

Android developers often desire a way to present a user with a method of selecting a file from "external" storage. Android's Intent system gives developers the ability to implicitly hook into other app's components, but if the user doesn't have a file explorer installed, the developer must instruct them to install one, or build one, themselves.

aFileChooser is an Android Library Project that simplifies this process.

Features:

  • Streamlines the Intent.ACTION_GET_CONTENT Intent calling process
  • Provides a built-in file explorer
  • Easily convert a URI into s java File object
  • Specify and determine MIME data types
  • Easily retrieve image thumbnails for media files
  • Follows Android conventions and is extremely simple to implement

Installation

Import aFileChooser and add it to your project as an Android Library Project. If you are unfamiliar with Android Library Projects, refer to the official documentation here.

Next, in your project, create an Activity that extends FileChooserActivity` and add it to your AndroidManifest.xml file.

Important The class extending FileChooserActivity must have the intent-filter set as seen bellow:

 <activity
        android:name=".FileChooserTestActivity"
        android:label="Choose a file" >
        <intent-filter>
            <action android:name="android.intent.action.GET_CONTENT" />
            
			<category android:name="android.intent.category.DEFAULT" />
            <category android:name="android.intent.category.OPENABLE" />
            
			<data android:mimeType="*/*" />
        </intent-filter>
</activity>

Note: The String used for android:label will be shown on the IntentChooser dialog.

Usage

To initiate the file selection, simply call showFileChooser() and listen for the selected file by overriding onFileSelect(). E.g.:

public class FileChooserTestActivity extends FileChooserActivity {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (!isIntentGetContent()) {
			showFileChooser();
		}
	}

	@Override
	protected void onFileSelect(File file) {
		// Here you handle the file selection.
	}
}

Important - FileChooserActivity uses Intent.ACTION_GET_CONTENT to show the embedded file explorer. Your Activity must check the Intent action, to ensure that it is not ACTION_GET_CONTENT.

A more robust implementation

public class FileChooserTestActivity extends FileChooserActivity 	{
	// TAG for log messages.
	private static final String TAG = "FileSelectorTestActivity";

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// We must check to ensure that the calling Intent is not Intent.ACTION_GET_CONTENT
		if (!isIntentGetContent()) {
			// Display the file chooser with all file types
			showFileChooser("*/*");
		}
	}

	@Override
	protected void onFileSelect(File file) {
		if (file != null) {
			final Context context = getApplicationContext();
		
			// Get the path of the Selected File.
			final String path = file.getAbsolutePath();
			Log.d(TAG, "File path: " + path);

			// Get the MIME type of the Selected File.			
			final String mimeType = FileUtils.getMimeType(context, file);
			Log.d(TAG, "File MIME type: " + mimeType);

			// Get the Uri of the Selected File
			// final Uri uri = Uri.fromFile(file);
			
			// Get the thumbnail of the Selected File, if image/video
			// final Bitmap bm = FileUtils.getThumbnail(context, uri, mimeType);

			// Here you can return any data from above to the calling Activity  
            finish();
		}	
	}

	@Override
	protected void onFileError(Exception e) {
		Log.e(TAG, "File select error", e);
		finish();
	}

	@Override
	protected void onFileSelectCancel() {
		Log.d(TAG, "File selections canceled");
		finish();
	}

	@Override
	protected void onFileDisconnect() {
		Log.d(TAG, "External storage disconneted");
		finish();
	}
}

Developed By

Paul Burke paulburke.co

License

Copyright 2012 Paul Burke

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:https://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.

About

Android library that provides a file explorer and allows users to select files on external storage.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published