Code Monkey home page Code Monkey logo

afilechooser's Introduction

aFileChooser - Android File Chooser

(Complete rewrite 10/30/2012)

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:

  • Provides a built-in file explorer
  • Streamlines the Intent.ACTION_GET_CONTENT Intent calling process
  • Easily convert a URI into s java File object
  • Determine MIME data types
  • Follows Android conventions (Fragments, Loaders) and is extremely simple to implement

screenshot-1 screenshot-2

Installation

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

Add FileChooserActivity to your project's AndroidManifest.xml file. The android:label and android:icon are displayed in the Action Chooser Dialog.

Important It must have the intent-filter set as seen bellow:

<activity
    android:name="com.ipaulpro.afilechooser.FileChooserActivity"
    android:icon="@drawable/ic_chooser"
    android:label="@string/choose_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>

Usage

aFileChooser uses startActivityForResult to return the Uri of the file selected. The FileUtils class provides a helper method to construct an ACTION_GET_CONTENT Intent that can be used to create the Action Chooser Dialog.

private static final int REQUEST_CODE = 1234;
private static final String CHOOSER_TITLE = "Select a file";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Intent target = FileUtils.createGetContentIntent();
    Intent intent = Intent.createChooser(target, CHOOSER_TITLE);
    try {
        startActivityForResult(intent, REQUEST_CODE);
    } catch (ActivityNotFoundException e) {
        // The reason for the existence of aFileChooser
    }
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
    case REQUEST_CODE:	
        if (resultCode == RESULT_OK) {	
            // The URI of the selected file	
            final Uri uri = data.getData();
            // Create a File from this Uri
            File file = FileUtils.getFile(uri);
        }
    }
}

A more robust example can be found in the aFileChooserExample folder.

Developed By

Paul Burke paulburke.co

License

Copyright (C) 2011 - 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://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.

---

FileUtils.java, MimeTypeParser.java and MimeTypes.java:

Copyright (C) 2007-2008 OpenIntents.org

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.

afilechooser's People

Contributors

abonander avatar ipaulpro avatar

Watchers

 avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.