API Change: MediaScannerConnection.ScanResultListener -> MediaScannerConnection.OnScanCompletedListener.

Change-Id: I1386394dbccda4c01e50d89b8fdeca97ee65a29f
This commit is contained in:
Ray Chen
2010-04-06 15:16:13 -07:00
parent 254f0e9bf7
commit c73455df1a

View File

@@ -48,17 +48,17 @@ import java.io.OutputStream;
*/ */
public class ExternalStorage extends Activity { public class ExternalStorage extends Activity {
ViewGroup mLayout; ViewGroup mLayout;
static class Item { static class Item {
View mRoot; View mRoot;
Button mCreate; Button mCreate;
Button mDelete; Button mDelete;
} }
Item mExternalStoragePublicPicture; Item mExternalStoragePublicPicture;
Item mExternalStoragePrivatePicture; Item mExternalStoragePrivatePicture;
Item mExternalStoragePrivateFile; Item mExternalStoragePrivateFile;
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@@ -114,10 +114,10 @@ public class ExternalStorage extends Activity {
} }
}); });
mLayout.addView(mExternalStoragePrivateFile.mRoot); mLayout.addView(mExternalStoragePrivateFile.mRoot);
startWatchingExternalStorage(); startWatchingExternalStorage();
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
@@ -135,12 +135,12 @@ public class ExternalStorage extends Activity {
mExternalStoragePrivateFile.mCreate.setEnabled(writeable && !has); mExternalStoragePrivateFile.mCreate.setEnabled(writeable && !has);
mExternalStoragePrivateFile.mDelete.setEnabled(writeable && has); mExternalStoragePrivateFile.mDelete.setEnabled(writeable && has);
} }
// BEGIN_INCLUDE(monitor_storage) // BEGIN_INCLUDE(monitor_storage)
BroadcastReceiver mExternalStorageReceiver; BroadcastReceiver mExternalStorageReceiver;
boolean mExternalStorageAvailable = false; boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false; boolean mExternalStorageWriteable = false;
void updateExternalStorageState() { void updateExternalStorageState() {
String state = Environment.getExternalStorageState(); String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) { if (Environment.MEDIA_MOUNTED.equals(state)) {
@@ -154,7 +154,7 @@ public class ExternalStorage extends Activity {
handleExternalStorageState(mExternalStorageAvailable, handleExternalStorageState(mExternalStorageAvailable,
mExternalStorageWriteable); mExternalStorageWriteable);
} }
void startWatchingExternalStorage() { void startWatchingExternalStorage() {
mExternalStorageReceiver = new BroadcastReceiver() { mExternalStorageReceiver = new BroadcastReceiver() {
@Override @Override
@@ -169,12 +169,12 @@ public class ExternalStorage extends Activity {
registerReceiver(mExternalStorageReceiver, filter); registerReceiver(mExternalStorageReceiver, filter);
updateExternalStorageState(); updateExternalStorageState();
} }
void stopWatchingExternalStorage() { void stopWatchingExternalStorage() {
unregisterReceiver(mExternalStorageReceiver); unregisterReceiver(mExternalStorageReceiver);
} }
// END_INCLUDE(monitor_storage) // END_INCLUDE(monitor_storage)
// BEGIN_INCLUDE(public_picture) // BEGIN_INCLUDE(public_picture)
void createExternalStoragePublicPicture() { void createExternalStoragePublicPicture() {
// Create a path where we will place our picture in the user's // Create a path where we will place our picture in the user's
@@ -185,11 +185,11 @@ public class ExternalStorage extends Activity {
File path = Environment.getExternalStoragePublicDirectory( File path = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES); Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg"); File file = new File(path, "DemoPicture.jpg");
try { try {
// Make sure the Pictures directory exists. // Make sure the Pictures directory exists.
path.mkdirs(); path.mkdirs();
// Very simple code to copy a picture from the application's // Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does // resource into the external file. Note that this code does
// no error checking, and assumes the picture is small (does not // no error checking, and assumes the picture is small (does not
@@ -202,12 +202,12 @@ public class ExternalStorage extends Activity {
os.write(data); os.write(data);
is.close(); is.close();
os.close(); os.close();
// Tell the media scanner about the new file so that it is // Tell the media scanner about the new file so that it is
// immediately available to the user. // immediately available to the user.
MediaScannerConnection.scanFile(this, MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null, new String[] { file.toString() }, null,
new MediaScannerConnection.ScanResultListener() { new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) { public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri); Log.i("ExternalStorage", "-> uri=" + uri);
@@ -219,7 +219,7 @@ public class ExternalStorage extends Activity {
Log.w("ExternalStorage", "Error writing " + file, e); Log.w("ExternalStorage", "Error writing " + file, e);
} }
} }
void deleteExternalStoragePublicPicture() { void deleteExternalStoragePublicPicture() {
// Create a path where we will place our picture in the user's // Create a path where we will place our picture in the user's
// public pictures directory and delete the file. If external // public pictures directory and delete the file. If external
@@ -229,7 +229,7 @@ public class ExternalStorage extends Activity {
File file = new File(path, "DemoPicture.jpg"); File file = new File(path, "DemoPicture.jpg");
file.delete(); file.delete();
} }
boolean hasExternalStoragePublicPicture() { boolean hasExternalStoragePublicPicture() {
// Create a path where we will place our picture in the user's // Create a path where we will place our picture in the user's
// public pictures directory and check if the file exists. If // public pictures directory and check if the file exists. If
@@ -241,7 +241,7 @@ public class ExternalStorage extends Activity {
return file.exists(); return file.exists();
} }
// END_INCLUDE(public_picture) // END_INCLUDE(public_picture)
// BEGIN_INCLUDE(private_picture) // BEGIN_INCLUDE(private_picture)
void createExternalStoragePrivatePicture() { void createExternalStoragePrivatePicture() {
// Create a path where we will place our picture in our own private // Create a path where we will place our picture in our own private
@@ -252,7 +252,7 @@ public class ExternalStorage extends Activity {
// your media for display to the user. // your media for display to the user.
File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES); File path = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File file = new File(path, "DemoPicture.jpg"); File file = new File(path, "DemoPicture.jpg");
try { try {
// Very simple code to copy a picture from the application's // Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does // resource into the external file. Note that this code does
@@ -266,12 +266,12 @@ public class ExternalStorage extends Activity {
os.write(data); os.write(data);
is.close(); is.close();
os.close(); os.close();
// Tell the media scanner about the new file so that it is // Tell the media scanner about the new file so that it is
// immediately available to the user. // immediately available to the user.
MediaScannerConnection.scanFile(this, MediaScannerConnection.scanFile(this,
new String[] { file.toString() }, null, new String[] { file.toString() }, null,
new MediaScannerConnection.ScanResultListener() { new MediaScannerConnection.OnScanCompletedListener() {
public void onScanCompleted(String path, Uri uri) { public void onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage", "Scanned " + path + ":"); Log.i("ExternalStorage", "Scanned " + path + ":");
Log.i("ExternalStorage", "-> uri=" + uri); Log.i("ExternalStorage", "-> uri=" + uri);
@@ -283,7 +283,7 @@ public class ExternalStorage extends Activity {
Log.w("ExternalStorage", "Error writing " + file, e); Log.w("ExternalStorage", "Error writing " + file, e);
} }
} }
void deleteExternalStoragePrivatePicture() { void deleteExternalStoragePrivatePicture() {
// Create a path where we will place our picture in the user's // Create a path where we will place our picture in the user's
// public pictures directory and delete the file. If external // public pictures directory and delete the file. If external
@@ -294,7 +294,7 @@ public class ExternalStorage extends Activity {
file.delete(); file.delete();
} }
} }
boolean hasExternalStoragePrivatePicture() { boolean hasExternalStoragePrivatePicture() {
// Create a path where we will place our picture in the user's // Create a path where we will place our picture in the user's
// public pictures directory and check if the file exists. If // public pictures directory and check if the file exists. If
@@ -308,13 +308,13 @@ public class ExternalStorage extends Activity {
return false; return false;
} }
// END_INCLUDE(private_picture) // END_INCLUDE(private_picture)
// BEGIN_INCLUDE(private_file) // BEGIN_INCLUDE(private_file)
void createExternalStoragePrivateFile() { void createExternalStoragePrivateFile() {
// Create a path where we will place our private file on external // Create a path where we will place our private file on external
// storage. // storage.
File file = new File(getExternalFilesDir(null), "DemoFile.jpg"); File file = new File(getExternalFilesDir(null), "DemoFile.jpg");
try { try {
// Very simple code to copy a picture from the application's // Very simple code to copy a picture from the application's
// resource into the external file. Note that this code does // resource into the external file. Note that this code does
@@ -334,7 +334,7 @@ public class ExternalStorage extends Activity {
Log.w("ExternalStorage", "Error writing " + file, e); Log.w("ExternalStorage", "Error writing " + file, e);
} }
} }
void deleteExternalStoragePrivateFile() { void deleteExternalStoragePrivateFile() {
// Get path for the file on external storage. If external // Get path for the file on external storage. If external
// storage is not currently mounted this will fail. // storage is not currently mounted this will fail.
@@ -343,7 +343,7 @@ public class ExternalStorage extends Activity {
file.delete(); file.delete();
} }
} }
boolean hasExternalStoragePrivateFile() { boolean hasExternalStoragePrivateFile() {
// Get path for the file on external storage. If external // Get path for the file on external storage. If external
// storage is not currently mounted this will fail. // storage is not currently mounted this will fail.
@@ -354,7 +354,7 @@ public class ExternalStorage extends Activity {
return false; return false;
} }
// END_INCLUDE(private_file) // END_INCLUDE(private_file)
Item createStorageControls(CharSequence label, File path, Item createStorageControls(CharSequence label, File path,
View.OnClickListener createClick, View.OnClickListener createClick,
View.OnClickListener deleteClick) { View.OnClickListener deleteClick) {