48
48
*/
49
49
public class ExternalStorage extends Activity {
50
50
ViewGroup mLayout ;
51
-
51
+
52
52
static class Item {
53
53
View mRoot ;
54
54
Button mCreate ;
55
55
Button mDelete ;
56
56
}
57
-
57
+
58
58
Item mExternalStoragePublicPicture ;
59
59
Item mExternalStoragePrivatePicture ;
60
60
Item mExternalStoragePrivateFile ;
61
-
61
+
62
62
@ Override
63
63
protected void onCreate (Bundle savedInstanceState ) {
64
64
super .onCreate (savedInstanceState );
@@ -114,10 +114,10 @@ public void onClick(View v) {
114
114
}
115
115
});
116
116
mLayout .addView (mExternalStoragePrivateFile .mRoot );
117
-
117
+
118
118
startWatchingExternalStorage ();
119
119
}
120
-
120
+
121
121
@ Override
122
122
protected void onDestroy () {
123
123
super .onDestroy ();
@@ -135,12 +135,12 @@ void handleExternalStorageState(boolean available, boolean writeable) {
135
135
mExternalStoragePrivateFile .mCreate .setEnabled (writeable && !has );
136
136
mExternalStoragePrivateFile .mDelete .setEnabled (writeable && has );
137
137
}
138
-
138
+
139
139
// BEGIN_INCLUDE(monitor_storage)
140
140
BroadcastReceiver mExternalStorageReceiver ;
141
141
boolean mExternalStorageAvailable = false ;
142
142
boolean mExternalStorageWriteable = false ;
143
-
143
+
144
144
void updateExternalStorageState () {
145
145
String state = Environment .getExternalStorageState ();
146
146
if (Environment .MEDIA_MOUNTED .equals (state )) {
@@ -154,7 +154,7 @@ void updateExternalStorageState() {
154
154
handleExternalStorageState (mExternalStorageAvailable ,
155
155
mExternalStorageWriteable );
156
156
}
157
-
157
+
158
158
void startWatchingExternalStorage () {
159
159
mExternalStorageReceiver = new BroadcastReceiver () {
160
160
@ Override
@@ -169,12 +169,12 @@ public void onReceive(Context context, Intent intent) {
169
169
registerReceiver (mExternalStorageReceiver , filter );
170
170
updateExternalStorageState ();
171
171
}
172
-
172
+
173
173
void stopWatchingExternalStorage () {
174
174
unregisterReceiver (mExternalStorageReceiver );
175
175
}
176
176
// END_INCLUDE(monitor_storage)
177
-
177
+
178
178
// BEGIN_INCLUDE(public_picture)
179
179
void createExternalStoragePublicPicture () {
180
180
// Create a path where we will place our picture in the user's
@@ -185,11 +185,11 @@ void createExternalStoragePublicPicture() {
185
185
File path = Environment .getExternalStoragePublicDirectory (
186
186
Environment .DIRECTORY_PICTURES );
187
187
File file = new File (path , "DemoPicture.jpg" );
188
-
188
+
189
189
try {
190
190
// Make sure the Pictures directory exists.
191
191
path .mkdirs ();
192
-
192
+
193
193
// Very simple code to copy a picture from the application's
194
194
// resource into the external file. Note that this code does
195
195
// no error checking, and assumes the picture is small (does not
@@ -202,12 +202,12 @@ void createExternalStoragePublicPicture() {
202
202
os .write (data );
203
203
is .close ();
204
204
os .close ();
205
-
205
+
206
206
// Tell the media scanner about the new file so that it is
207
207
// immediately available to the user.
208
208
MediaScannerConnection .scanFile (this ,
209
209
new String [] { file .toString () }, null ,
210
- new MediaScannerConnection .ScanResultListener () {
210
+ new MediaScannerConnection .OnScanCompletedListener () {
211
211
public void onScanCompleted (String path , Uri uri ) {
212
212
Log .i ("ExternalStorage" , "Scanned " + path + ":" );
213
213
Log .i ("ExternalStorage" , "-> uri=" + uri );
@@ -219,7 +219,7 @@ public void onScanCompleted(String path, Uri uri) {
219
219
Log .w ("ExternalStorage" , "Error writing " + file , e );
220
220
}
221
221
}
222
-
222
+
223
223
void deleteExternalStoragePublicPicture () {
224
224
// Create a path where we will place our picture in the user's
225
225
// public pictures directory and delete the file. If external
@@ -229,7 +229,7 @@ void deleteExternalStoragePublicPicture() {
229
229
File file = new File (path , "DemoPicture.jpg" );
230
230
file .delete ();
231
231
}
232
-
232
+
233
233
boolean hasExternalStoragePublicPicture () {
234
234
// Create a path where we will place our picture in the user's
235
235
// public pictures directory and check if the file exists. If
@@ -241,7 +241,7 @@ boolean hasExternalStoragePublicPicture() {
241
241
return file .exists ();
242
242
}
243
243
// END_INCLUDE(public_picture)
244
-
244
+
245
245
// BEGIN_INCLUDE(private_picture)
246
246
void createExternalStoragePrivatePicture () {
247
247
// Create a path where we will place our picture in our own private
@@ -252,7 +252,7 @@ void createExternalStoragePrivatePicture() {
252
252
// your media for display to the user.
253
253
File path = getExternalFilesDir (Environment .DIRECTORY_PICTURES );
254
254
File file = new File (path , "DemoPicture.jpg" );
255
-
255
+
256
256
try {
257
257
// Very simple code to copy a picture from the application's
258
258
// resource into the external file. Note that this code does
@@ -266,12 +266,12 @@ void createExternalStoragePrivatePicture() {
266
266
os .write (data );
267
267
is .close ();
268
268
os .close ();
269
-
269
+
270
270
// Tell the media scanner about the new file so that it is
271
271
// immediately available to the user.
272
272
MediaScannerConnection .scanFile (this ,
273
273
new String [] { file .toString () }, null ,
274
- new MediaScannerConnection .ScanResultListener () {
274
+ new MediaScannerConnection .OnScanCompletedListener () {
275
275
public void onScanCompleted (String path , Uri uri ) {
276
276
Log .i ("ExternalStorage" , "Scanned " + path + ":" );
277
277
Log .i ("ExternalStorage" , "-> uri=" + uri );
@@ -283,7 +283,7 @@ public void onScanCompleted(String path, Uri uri) {
283
283
Log .w ("ExternalStorage" , "Error writing " + file , e );
284
284
}
285
285
}
286
-
286
+
287
287
void deleteExternalStoragePrivatePicture () {
288
288
// Create a path where we will place our picture in the user's
289
289
// public pictures directory and delete the file. If external
@@ -294,7 +294,7 @@ void deleteExternalStoragePrivatePicture() {
294
294
file .delete ();
295
295
}
296
296
}
297
-
297
+
298
298
boolean hasExternalStoragePrivatePicture () {
299
299
// Create a path where we will place our picture in the user's
300
300
// public pictures directory and check if the file exists. If
@@ -308,13 +308,13 @@ boolean hasExternalStoragePrivatePicture() {
308
308
return false ;
309
309
}
310
310
// END_INCLUDE(private_picture)
311
-
311
+
312
312
// BEGIN_INCLUDE(private_file)
313
313
void createExternalStoragePrivateFile () {
314
314
// Create a path where we will place our private file on external
315
315
// storage.
316
316
File file = new File (getExternalFilesDir (null ), "DemoFile.jpg" );
317
-
317
+
318
318
try {
319
319
// Very simple code to copy a picture from the application's
320
320
// resource into the external file. Note that this code does
@@ -334,7 +334,7 @@ void createExternalStoragePrivateFile() {
334
334
Log .w ("ExternalStorage" , "Error writing " + file , e );
335
335
}
336
336
}
337
-
337
+
338
338
void deleteExternalStoragePrivateFile () {
339
339
// Get path for the file on external storage. If external
340
340
// storage is not currently mounted this will fail.
@@ -343,7 +343,7 @@ void deleteExternalStoragePrivateFile() {
343
343
file .delete ();
344
344
}
345
345
}
346
-
346
+
347
347
boolean hasExternalStoragePrivateFile () {
348
348
// Get path for the file on external storage. If external
349
349
// storage is not currently mounted this will fail.
@@ -354,7 +354,7 @@ boolean hasExternalStoragePrivateFile() {
354
354
return false ;
355
355
}
356
356
// END_INCLUDE(private_file)
357
-
357
+
358
358
Item createStorageControls (CharSequence label , File path ,
359
359
View .OnClickListener createClick ,
360
360
View .OnClickListener deleteClick ) {
0 commit comments