Recipe 10.5 Saving Files to External Storage
The previous recipes show you how to save data to the application’s filesystem on the device. In some cases, you might want to save the files to the device’s external storage, such as the SD card.
Solution
Before you write or read files from the external storage, it is always important to check whether it is available and writeable. The following method checks that:
package net.learn2develop.externalstorage;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public boolean IsExternalStorageAvailableAndWriteable() {
boolean externalStorageAvailable = false;
boolean externalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
//---you ...
Get Android Application Development Cookbook: 93 Recipes for Building Winning Apps now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.