Mercurial > directorypicker
comparison src/net/bgreco/DirectoryPicker.java @ 2:a684ded7d920 default tip
Prevent crash when not passed a bundle. Thanks to Curtis Tuplin for the fix.
author | Brad Greco |
---|---|
date | Wed, 30 May 2012 21:33:07 -0500 |
parents | adceb82e0818 |
children |
comparison
equal
deleted
inserted
replaced
1:adceb82e0818 | 2:a684ded7d920 |
---|---|
1 package net.bgreco; | 1 package net.bgreco; |
2 | |
3 import java.io.File; | |
4 import java.util.ArrayList; | |
5 import java.util.Collections; | |
6 | |
7 import android.app.ListActivity; | |
8 import android.content.Context; | |
9 import android.content.Intent; | |
10 import android.os.Bundle; | |
11 import android.os.Environment; | |
12 import android.view.View; | |
13 import android.widget.AdapterView; | |
14 import android.widget.AdapterView.OnItemClickListener; | |
15 import android.widget.ArrayAdapter; | |
16 import android.widget.Button; | |
17 import android.widget.ListView; | |
18 import android.widget.Toast; | |
19 | |
2 /** | 20 /** |
3 Copyright (C) 2011 by Brad Greco <brad@bgreco.net> | 21 Copyright (C) 2011 by Brad Greco <brad@bgreco.net> |
4 | 22 |
5 Permission is hereby granted, free of charge, to any person obtaining a copy | 23 Permission is hereby granted, free of charge, to any person obtaining a copy |
6 of this software and associated documentation files (the "Software"), to deal | 24 of this software and associated documentation files (the "Software"), to deal |
18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | 36 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | 37 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | 38 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
21 THE SOFTWARE. | 39 THE SOFTWARE. |
22 */ | 40 */ |
23 import java.io.File; | |
24 import java.util.ArrayList; | |
25 import java.util.Collections; | |
26 | |
27 import android.app.ListActivity; | |
28 import android.content.Context; | |
29 import android.content.Intent; | |
30 import android.os.Bundle; | |
31 import android.os.Environment; | |
32 import android.view.View; | |
33 import android.widget.AdapterView; | |
34 import android.widget.AdapterView.OnItemClickListener; | |
35 import android.widget.ArrayAdapter; | |
36 import android.widget.Button; | |
37 import android.widget.ListView; | |
38 import android.widget.Toast; | |
39 | 41 |
40 public class DirectoryPicker extends ListActivity { | 42 public class DirectoryPicker extends ListActivity { |
41 | 43 |
42 public static final String START_DIR = "startDir"; | 44 public static final String START_DIR = "startDir"; |
43 public static final String ONLY_DIRS = "onlyDirs"; | 45 public static final String ONLY_DIRS = "onlyDirs"; |
44 public static final String SHOW_HIDDEN = "showHidden"; | 46 public static final String SHOW_HIDDEN = "showHidden"; |
45 public static final String CHOSEN_DIRECTORY = "chosenDir"; | 47 public static final String CHOSEN_DIRECTORY = "chosenDir"; |
46 public static final int PICK_DIRECTORY = 43522432; | 48 public static final int PICK_DIRECTORY = 43522432; |
47 private File dir; | 49 private File dir; |
50 private boolean showHidden = false; | |
51 private boolean onlyDirs = true ; | |
48 | 52 |
49 @Override | 53 @Override |
50 public void onCreate(Bundle savedInstanceState) { | 54 public void onCreate(Bundle savedInstanceState) { |
51 super.onCreate(savedInstanceState); | 55 super.onCreate(savedInstanceState); |
52 Bundle extras = getIntent().getExtras(); | 56 Bundle extras = getIntent().getExtras(); |
53 dir = Environment.getExternalStorageDirectory(); | 57 dir = Environment.getExternalStorageDirectory(); |
54 String preferredStartDir = extras.getString(START_DIR); | 58 if (extras != null) { |
55 final boolean showHidden = extras.getBoolean(SHOW_HIDDEN, false); | 59 String preferredStartDir = extras.getString(START_DIR); |
56 final boolean onlyDirs = extras.getBoolean(ONLY_DIRS, true); | 60 showHidden = extras.getBoolean(SHOW_HIDDEN, false); |
57 if(preferredStartDir != null) { | 61 onlyDirs = extras.getBoolean(ONLY_DIRS, true); |
58 File startDir = new File(preferredStartDir); | 62 if(preferredStartDir != null) { |
59 if(startDir.isDirectory()) { | 63 File startDir = new File(preferredStartDir); |
60 dir = startDir; | 64 if(startDir.isDirectory()) { |
61 } | 65 dir = startDir; |
66 } | |
67 } | |
62 } | 68 } |
63 | 69 |
64 setContentView(R.layout.chooser_list); | 70 setContentView(R.layout.chooser_list); |
65 setTitle(dir.getAbsolutePath()); | 71 setTitle(dir.getAbsolutePath()); |
66 Button btnChoose = (Button) findViewById(R.id.btnChoose); | 72 Button btnChoose = (Button) findViewById(R.id.btnChoose); |