Skip to content
This repository was archived by the owner on Mar 30, 2022. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,14 @@ Use `startActivityForResult(Intent, int)` to launch the "Intent Chooser" dialog
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

Intent getContentIntent = FileUtils.createGetContentIntent();
Intent intent = Intent.createChooser(getContentIntent, "Select a file");
Intent intent = new Intent(this, FileChooserActivity.class);

//Add the following statement only if you want to set a custom root path for the chooser:
intent.putExtra(FileChooserActivity.PATH, "/your/custom/path");

//Add the following statement only if you want to enable the user to select only a single file type
intent.putExtra(FileChooserActivity.ALLOWED_FILE_EXTENSION, "pdf")

startActivityForResult(intent, REQUEST_CHOOSER);
}

Expand Down
1 change: 1 addition & 0 deletions aFileChooser/.classpath
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,6 @@
<classpathentry kind="src" path="src"/>
<classpathentry kind="src" path="gen"/>
<classpathentry kind="lib" path="libs/android-support-v4.jar"/>
<classpathentry exported="true" kind="con" path="com.android.ide.eclipse.adt.DEPENDENCIES"/>
<classpathentry kind="output" path="bin/classes"/>
</classpath>
4 changes: 4 additions & 0 deletions aFileChooser/.settings/org.eclipse.jdt.core.prefs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
eclipse.preferences.version=1
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.6
org.eclipse.jdt.core.compiler.compliance=1.6
org.eclipse.jdt.core.compiler.source=1.6
Binary file modified aFileChooser/libs/android-support-v4.jar
Binary file not shown.
120 changes: 74 additions & 46 deletions aFileChooser/src/com/ipaulpro/afilechooser/FileChooserActivity.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*
/*
* Copyright (C) 2013 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.
*/
*
* 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.
*/

package com.ipaulpro.afilechooser;

import java.io.File;

import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.Context;
Expand All @@ -34,36 +37,37 @@
import android.view.MenuItem;
import android.widget.Toast;

import java.io.File;

/**
* Main Activity that handles the FileListFragments
*
* Main Activity that handles the FileListFragments
*
* @version 2013-06-25
*
*
* @author paulburke (ipaulpro)
*
* @author alexbbb
*/
@SuppressLint("NewApi")
public class FileChooserActivity extends FragmentActivity implements
OnBackStackChangedListener {

public static final String PATH = "path";
public static final String ALLOWED_FILE_EXTENSION = "allowedFileExtension";
public static final String EXTERNAL_BASE_PATH = Environment
.getExternalStorageDirectory().getAbsolutePath();

private static final boolean HAS_ACTIONBAR = Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB;

private FragmentManager mFragmentManager;
private BroadcastReceiver mStorageListener = new BroadcastReceiver() {
private final BroadcastReceiver mStorageListener = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Toast.makeText(context, R.string.storage_removed, Toast.LENGTH_LONG).show();
finishWithResult(null);
}
};

private String mPath;

private String mAllowedExtension;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -73,11 +77,34 @@ protected void onCreate(Bundle savedInstanceState) {
mFragmentManager = getSupportFragmentManager();
mFragmentManager.addOnBackStackChangedListener(this);

if (savedInstanceState == null) {
mPath = EXTERNAL_BASE_PATH;
addFragment();
} else {
mPath = savedInstanceState.getString(PATH);
Intent intent = getIntent();
if (intent != null) {
final String customPath = intent.getStringExtra(PATH);
if (customPath != null && !customPath.equals("")) {
mPath = customPath;
}

final String allowedExtension = intent.getStringExtra(ALLOWED_FILE_EXTENSION);
if (allowedExtension != null) {
mAllowedExtension = allowedExtension;
}
}

if (mPath == null) {
if (savedInstanceState == null) {
mPath = EXTERNAL_BASE_PATH;
addFragment();
} else {
mPath = savedInstanceState.getString(PATH);
}
}

if (mAllowedExtension == null) {
if (savedInstanceState == null) {
mAllowedExtension = "";
} else {
mAllowedExtension = savedInstanceState.getString(ALLOWED_FILE_EXTENSION);
}
}

setTitle(mPath);
Expand All @@ -86,52 +113,53 @@ protected void onCreate(Bundle savedInstanceState) {
@Override
protected void onPause() {
super.onPause();

unregisterStorageListener();
}

@Override
protected void onResume() {
super.onResume();

registerStorageListener();
}

@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);

outState.putString(PATH, mPath);
outState.putString(ALLOWED_FILE_EXTENSION, mAllowedExtension);
}

@Override
public void onBackStackChanged() {

int count = mFragmentManager.getBackStackEntryCount();
if (count > 0) {
BackStackEntry fragment = mFragmentManager.getBackStackEntryAt(count - 1);
mPath = fragment.getName();
} else {
mPath = EXTERNAL_BASE_PATH;
}

setTitle(mPath);
if (HAS_ACTIONBAR) invalidateOptionsMenu();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (HAS_ACTIONBAR) {
boolean hasBackStack = mFragmentManager.getBackStackEntryCount() > 0;

ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(hasBackStack);
actionBar.setHomeButtonEnabled(hasBackStack);
}

return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
Expand All @@ -147,21 +175,21 @@ public boolean onOptionsItemSelected(MenuItem item) {
* Add the initial Fragment with given path.
*/
private void addFragment() {
FileListFragment fragment = FileListFragment.newInstance(mPath);
FileListFragment fragment = FileListFragment.newInstance(mPath, mAllowedExtension);
mFragmentManager.beginTransaction()
.add(R.id.explorer_fragment, fragment).commit();
}

/**
* "Replace" the existing Fragment with a new one using given path.
* We're really adding a Fragment to the back stack.
*
*
* @param file The file (directory) to display.
*/
private void replaceFragment(File file) {
mPath = file.getAbsolutePath();

FileListFragment fragment = FileListFragment.newInstance(mPath);
FileListFragment fragment = FileListFragment.newInstance(mPath, mAllowedExtension);
mFragmentManager.beginTransaction()
.replace(R.id.explorer_fragment, fragment)
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
Expand All @@ -170,7 +198,7 @@ private void replaceFragment(File file) {

/**
* Finish this Activity with a result code and URI of the selected file.
*
*
* @param file The file selected.
*/
private void finishWithResult(File file) {
Expand All @@ -179,28 +207,28 @@ private void finishWithResult(File file) {
setResult(RESULT_OK, new Intent().setData(uri));
finish();
} else {
setResult(RESULT_CANCELED);
setResult(RESULT_CANCELED);
finish();
}
}

/**
* Called when the user selects a File
*
*
* @param file The file that was selected
*/
protected void onFileSelected(File file) {
if (file != null) {
if (file.isDirectory()) {
replaceFragment(file);
} else {
finishWithResult(file);
finishWithResult(file);
}
} else {
Toast.makeText(FileChooserActivity.this, R.string.error_selecting_file, Toast.LENGTH_SHORT).show();
}
}

/**
* Register the external storage BroadcastReceiver.
*/
Expand Down
55 changes: 30 additions & 25 deletions aFileChooser/src/com/ipaulpro/afilechooser/FileListFragment.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
/*
/*
* Copyright (C) 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.
*/
*
* 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.
*/

package com.ipaulpro.afilechooser;

import java.io.File;
import java.util.List;

import android.os.Bundle;
import android.os.Environment;
import android.support.v4.app.ListFragment;
Expand All @@ -24,16 +27,13 @@
import android.view.View;
import android.widget.ListView;

import java.io.File;
import java.util.List;

/**
* Fragment that displays a list of Files in a given path.
*
*
* @version 2012-10-28
*
*
* @author paulburke (ipaulpro)
*
*
*/
public class FileListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<List<File>> {
Expand All @@ -42,17 +42,19 @@ public class FileListFragment extends ListFragment implements

private FileListAdapter mAdapter;
private String mPath;
private String mAllowedFileExtension;

/**
* Create a new instance with the given file path.
*
*
* @param path The absolute path of the file (directory) to display.
* @return A new Fragment with the given file path.
* @return A new Fragment with the given file path.
*/
public static FileListFragment newInstance(String path) {
public static FileListFragment newInstance(String path, String allowedFiles) {
FileListFragment fragment = new FileListFragment();
Bundle args = new Bundle();
args.putString(FileChooserActivity.PATH, path);
args.putString(FileChooserActivity.ALLOWED_FILE_EXTENSION, allowedFiles);
fragment.setArguments(args);

return fragment;
Expand All @@ -66,6 +68,9 @@ public void onCreate(Bundle savedInstanceState) {
mPath = getArguments() != null ? getArguments().getString(
FileChooserActivity.PATH) : Environment
.getExternalStorageDirectory().getAbsolutePath();

mAllowedFileExtension = getArguments() != null ? getArguments().getString(
FileChooserActivity.ALLOWED_FILE_EXTENSION) : "";
}

@Override
Expand All @@ -75,7 +80,7 @@ public void onActivityCreated(Bundle savedInstanceState) {
setListShown(false);

getLoaderManager().initLoader(LOADER_ID, null, this);

super.onActivityCreated(savedInstanceState);
}

Expand All @@ -91,7 +96,7 @@ public void onListItemClick(ListView l, View v, int position, long id) {

@Override
public Loader<List<File>> onCreateLoader(int id, Bundle args) {
return new FileLoader(getActivity(), mPath);
return new FileLoader(getActivity(), mPath, mAllowedFileExtension);
}

@Override
Expand Down
Loading