ExternalStorage.
java
The file containing the source code shown below is located in the corresponding directory in
<sdk>/platforms/android-<version>/samples/...
/*
* Copyright (C) 2010 The Android Open
Source Project
*
* 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
*
*
[Link]
*
* 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 [Link];
//Need the following import to get access to
the app resources, since this
//class is in a sub-package.
import [Link].R;
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
/**
* Demonstration of styled text resources.
*/
public class ExternalStorage extends
Activity {
ViewGroup mLayout;
static class Item {
View mRoot;
Button mCreate;
Button mDelete;
}
Item mExternalStoragePublicPicture;
Item mExternalStoragePrivatePicture;
Item mExternalStoragePrivateFile;
@Override
protected void onCreate(Bundle
savedInstanceState) {
[Link](savedInstanceState);
setContentView([Link].external_storage);
mLayout =
(ViewGroup)findViewById([Link]);
mExternalStoragePublicPicture =
createStorageControls(
"Picture:
getExternalStoragePublicDirectory",
[Link]
y(
Environment.DIRECTORY_PICTURES),
new [Link]() {
public void onClick(View
v) {
createExternalStoragePublicPicture();
updateExternalStorageState();
}
},
new [Link]() {
public void onClick(View
v) {
deleteExternalStoragePublicPicture();
updateExternalStorageState();
}
});
[Link](mExternalStoragePublicPictur
[Link]);
mExternalStoragePrivatePicture =
createStorageControls(
"Picture
getExternalFilesDir",
getExternalFilesDir(Environment.DIRECTORY_PI
CTURES),
new [Link]() {
public void onClick(View
v) {
createExternalStoragePrivatePicture();
updateExternalStorageState();
}
},
new [Link]() {
public void onClick(View
v) {
deleteExternalStoragePrivatePicture();
updateExternalStorageState();
}
});
[Link](mExternalStoragePrivatePictu
[Link]);
mExternalStoragePrivateFile =
createStorageControls(
"File getExternalFilesDir",
getExternalFilesDir(null),
new [Link]() {
public void onClick(View
v) {
createExternalStoragePrivateFile();
updateExternalStorageState();
}
},
new [Link]() {
public void onClick(View
v) {
deleteExternalStoragePrivateFile();
updateExternalStorageState();
}
});
[Link](mExternalStoragePrivateFile.
mRoot);
startWatchingExternalStorage();
}
@Override
protected void onDestroy() {
[Link]();
stopWatchingExternalStorage();
}
void handleExternalStorageState(boolean
available, boolean writeable) {
boolean has =
hasExternalStoragePublicPicture();
[Link]
bled(writeable && !has);
[Link]
bled(writeable && has);
has =
hasExternalStoragePrivatePicture();
[Link]
abled(writeable && !has);
[Link]
abled(writeable && has);
has =
hasExternalStoragePrivateFile();
[Link]
ed(writeable && !has);
[Link]
ed(writeable && has);
}
BroadcastReceiver
mExternalStorageReceiver;
boolean mExternalStorageAvailable =
false;
boolean mExternalStorageWriteable =
false;
void updateExternalStorageState() {
String state =
[Link]();
if
(Environment.MEDIA_MOUNTED.equals(state)) {
mExternalStorageAvailable =
mExternalStorageWriteable = true;
} else if
(Environment.MEDIA_MOUNTED_READ_ONLY.equals(
state)) {
mExternalStorageAvailable =
true;
mExternalStorageWriteable =
false;
} else {
mExternalStorageAvailable =
mExternalStorageWriteable = false;
}
handleExternalStorageState(mExternalStorageA
vailable,
mExternalStorageWriteable);
}
void startWatchingExternalStorage() {
mExternalStorageReceiver = new
BroadcastReceiver() {
@Override
public void onReceive(Context
context, Intent intent) {
Log.i("test", "Storage: " +
[Link]());
updateExternalStorageState();
}
};
IntentFilter filter = new
IntentFilter();
[Link](Intent.ACTION_MEDIA_MOUNTED
);
[Link](Intent.ACTION_MEDIA_REMOVED
);
registerReceiver(mExternalStorageReceiver,
filter);
updateExternalStorageState();
}
void stopWatchingExternalStorage() {
unregisterReceiver(mExternalStorageReceiver)
;
}
void
createExternalStoragePublicPicture() {
// Create a path where we will place
our picture in the user's
// public pictures directory. Note
that you should be careful about
// what you place here, since the
user often manages these files. For
// pictures and other media owned by
the application, consider
// [Link]().
File path =
[Link]
y(
Environment.DIRECTORY_PICTURES);
File file = new File(path,
"[Link]");
try {
// Make sure the Pictures
directory exists.
[Link]();
// Very simple code to copy a
picture from the application's
// resource into the external
file. Note that this code does
// no error checking, and
assumes the picture is small (does not
// try to copy it in chunks).
Note that if external storage is
// not currently mounted this
will silently fail.
InputStream is =
getResources().openRawResource([Link]
lloons);
OutputStream os = new
FileOutputStream(file);
byte[] data = new
byte[[Link]()];
[Link](data);
[Link](data);
[Link]();
[Link]();
// Tell the media scanner about
the new file so that it is
// immediately available to the
user.
[Link](this,
new String[]
{ [Link]() }, null,
new
[Link]
er() {
public void
onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage",
"Scanned " + path + ":");
Log.i("ExternalStorage",
"-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely
because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error
writing " + file, e);
}
}
void
deleteExternalStoragePublicPicture() {
// Create a path where we will place
our picture in the user's
// public pictures directory and
delete the file. If external
// storage is not currently mounted
this will fail.
File path =
[Link]
y(
Environment.DIRECTORY_PICTURES);
File file = new File(path,
"[Link]");
[Link]();
}
boolean
hasExternalStoragePublicPicture() {
// Create a path where we will place
our picture in the user's
// public pictures directory and
check if the file exists. If
// external storage is not currently
mounted this will think the
// picture doesn't exist.
File path =
[Link]
y(
Environment.DIRECTORY_PICTURES);
File file = new File(path,
"[Link]");
return [Link]();
}
void
createExternalStoragePrivatePicture() {
// Create a path where we will place
our picture in our own private
// pictures directory. Note that we
don't really need to place a
// picture in DIRECTORY_PICTURES,
since the media scanner will see
// all media in these directories;
this may be useful with other
// media types such as
DIRECTORY_MUSIC however to help it classify
// your media for display to the
user.
File path =
getExternalFilesDir(Environment.DIRECTORY_PI
CTURES);
File file = new File(path,
"[Link]");
try {
// Very simple code to copy a
picture from the application's
// resource into the external
file. Note that this code does
// no error checking, and
assumes the picture is small (does not
// try to copy it in chunks).
Note that if external storage is
// not currently mounted this
will silently fail.
InputStream is =
getResources().openRawResource([Link]
lloons);
OutputStream os = new
FileOutputStream(file);
byte[] data = new
byte[[Link]()];
[Link](data);
[Link](data);
[Link]();
[Link]();
// Tell the media scanner about
the new file so that it is
// immediately available to the
user.
[Link](this,
new String[]
{ [Link]() }, null,
new
[Link]
er() {
public void
onScanCompleted(String path, Uri uri) {
Log.i("ExternalStorage",
"Scanned " + path + ":");
Log.i("ExternalStorage",
"-> uri=" + uri);
}
});
} catch (IOException e) {
// Unable to create file, likely
because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error
writing " + file, e);
}
}
void
deleteExternalStoragePrivatePicture() {
// Create a path where we will place
our picture in the user's
// public pictures directory and
delete the file. If external
// storage is not currently mounted
this will fail.
File path =
getExternalFilesDir(Environment.DIRECTORY_PI
CTURES);
if (path != null) {
File file = new File(path,
"[Link]");
[Link]();
}
}
boolean
hasExternalStoragePrivatePicture() {
// Create a path where we will place
our picture in the user's
// public pictures directory and
check if the file exists. If
// external storage is not currently
mounted this will think the
// picture doesn't exist.
File path =
getExternalFilesDir(Environment.DIRECTORY_PI
CTURES);
if (path != null) {
File file = new File(path,
"[Link]");
return [Link]();
}
return false;
}
void createExternalStoragePrivateFile()
{
// Create a path where we will
place our private file on external
// storage.
File file = new
File(getExternalFilesDir(null),
"[Link]");
try {
// Very simple code to copy a
picture from the application's
// resource into the external
file. Note that this code does
// no error checking, and
assumes the picture is small (does not
// try to copy it in chunks).
Note that if external storage is
// not currently mounted this
will silently fail.
InputStream is =
getResources().openRawResource([Link]
lloons);
OutputStream os = new
FileOutputStream(file);
byte[] data = new
byte[[Link]()];
[Link](data);
[Link](data);
[Link]();
[Link]();
} catch (IOException e) {
// Unable to create file,
likely because external storage is
// not currently mounted.
Log.w("ExternalStorage", "Error
writing " + file, e);
}
}
void deleteExternalStoragePrivateFile()
{
// Get path for the file on
external storage. If external
// storage is not currently mounted
this will fail.
File file = new
File(getExternalFilesDir(null),
"[Link]");
if (file != null) {
[Link]();
}
}
boolean hasExternalStoragePrivateFile()
{
// Get path for the file on
external storage. If external
// storage is not currently mounted
this will fail.
File file = new
File(getExternalFilesDir(null),
"[Link]");
if (file != null) {
return [Link]();
}
return false;
}
Item createStorageControls(CharSequence
label, File path,
[Link]
createClick,
[Link]
deleteClick) {
LayoutInflater inflater =
(LayoutInflater)getSystemService(LAYOUT_INFL
ATER_SERVICE);
Item item = new Item();
[Link] =
[Link]([Link].external_storage_i
tem, null);
TextView tv =
(TextView)[Link]([Link]
);
[Link](label);
if (path != null) {
tv =
(TextView)[Link]([Link])
;
[Link]([Link]());
}
[Link] =
(Button)[Link]([Link])
;
[Link](createClick)
;
[Link] =
(Button)[Link]([Link])
;
[Link](deleteClick)
;
return item;
}
}