Some notes on writing to a file owned by an app in SD card on Android
January 13, 2012 14:10:42 Last update: January 13, 2012 14:10:42
For some reason I don't know writing to files in Android is very complicated and tedious. Here is some code for those who might need it.
Apps should write in the SD in the directory /Android/data/package_name/files/ so that it is deleted with the app uninstall.
package randomname;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
public class FileIOLibrary {
String packageName;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
FileIOLibrary(String packageNamep)
{
this.packageName=packageNamep;
}
public boolean isExternalStorageAvailable()
{
updateExternalStorageState();
return mExternalStorageAvailable;
}
public boolean isExternalStorageWritable()
{
updateExternalStorageState();
return mExternalStorageWriteable;
}
/**
* writes the current state of SD card to the corresponding variables
*/
void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
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(mExternalStorageAvailable,
// mExternalStorageWriteable);
Log.d("DEBUG", "Files mExternalStorageAvailable: "+mExternalStorageAvailable);
Log.d("DEBUG", "Files mExternalStorageWriteable: "+mExternalStorageWriteable);
}
/**
* returns the directory to write files in the SD card for this app
* @return
*/
public String getFilesDir()
{
return Environment.getExternalStorageDirectory()+
"/Android/data/"+this.packageName+"/files/";
}
/**
* returns if a dir/file exists within the specific place inside the sd card for this app
* @param filenamep
* @return
*/
public boolean fileExists(String filenamep)
{
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
// create file
File fn=new File(f, filenamep);
return fn.exists();
}
/**
* creates an empty file within the specific place inside the sd card for this app
* @param filenamep
* @return
*/
public boolean createEmptyFile(String filenamep)
{
String path="/Android/data/"+this.packageName+"/files/";
try
{
File f= new File(Environment.getExternalStorageDirectory(), path);
f.mkdirs(); // create directory structure
// create file
File fn=new File(f, filenamep);
fn.createNewFile();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public boolean appendTextToFile(String filenamep, String textp)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileWriter outf = new FileWriter(new File(f, filenamep), true); // final parameter=append
outf.append(textp);
outf.flush();
outf.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public boolean WriteBinaryDataToFile(String filenamep, ByteBuffer data)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileOutputStream outs = new FileOutputStream(new File(f, filenamep), true);
outs.write(data.array(), 0, data.array().length);
outs.flush();
outs.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public ByteBuffer ReadBinaryDataFromToFile(String filenamep, int sizep)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
ByteBuffer data = ByteBuffer.allocate(sizep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileInputStream ins = new FileInputStream(new File(f, filenamep));
ins.read(data.array(), 0, data.array().length);
ins.close();
return data;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}
Apps should write in the SD in the directory /Android/data/package_name/files/ so that it is deleted with the app uninstall.
package randomname;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.ByteBuffer;
import android.content.Context;
import android.os.Environment;
import android.util.Log;
public class FileIOLibrary {
String packageName;
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
FileIOLibrary(String packageNamep)
{
this.packageName=packageNamep;
}
public boolean isExternalStorageAvailable()
{
updateExternalStorageState();
return mExternalStorageAvailable;
}
public boolean isExternalStorageWritable()
{
updateExternalStorageState();
return mExternalStorageWriteable;
}
/**
* writes the current state of SD card to the corresponding variables
*/
void updateExternalStorageState() {
String state = Environment.getExternalStorageState();
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(mExternalStorageAvailable,
// mExternalStorageWriteable);
Log.d("DEBUG", "Files mExternalStorageAvailable: "+mExternalStorageAvailable);
Log.d("DEBUG", "Files mExternalStorageWriteable: "+mExternalStorageWriteable);
}
/**
* returns the directory to write files in the SD card for this app
* @return
*/
public String getFilesDir()
{
return Environment.getExternalStorageDirectory()+
"/Android/data/"+this.packageName+"/files/";
}
/**
* returns if a dir/file exists within the specific place inside the sd card for this app
* @param filenamep
* @return
*/
public boolean fileExists(String filenamep)
{
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
// create file
File fn=new File(f, filenamep);
return fn.exists();
}
/**
* creates an empty file within the specific place inside the sd card for this app
* @param filenamep
* @return
*/
public boolean createEmptyFile(String filenamep)
{
String path="/Android/data/"+this.packageName+"/files/";
try
{
File f= new File(Environment.getExternalStorageDirectory(), path);
f.mkdirs(); // create directory structure
// create file
File fn=new File(f, filenamep);
fn.createNewFile();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public boolean appendTextToFile(String filenamep, String textp)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileWriter outf = new FileWriter(new File(f, filenamep), true); // final parameter=append
outf.append(textp);
outf.flush();
outf.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public boolean WriteBinaryDataToFile(String filenamep, ByteBuffer data)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileOutputStream outs = new FileOutputStream(new File(f, filenamep), true);
outs.write(data.array(), 0, data.array().length);
outs.flush();
outs.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
public ByteBuffer ReadBinaryDataFromToFile(String filenamep, int sizep)
{
try
{
if (!this.fileExists(filenamep))
this.createEmptyFile(filenamep);
ByteBuffer data = ByteBuffer.allocate(sizep);
String path="/Android/data/"+this.packageName+"/files/";
File f= new File(Environment.getExternalStorageDirectory(), path);
FileInputStream ins = new FileInputStream(new File(f, filenamep));
ins.read(data.array(), 0, data.array().length);
ins.close();
return data;
}
catch (Exception e)
{
e.printStackTrace();
return null;
}
}
}