Writing and Deleting on the SD card, simplified

This should be something easy and intuitive. The internet makes this not so. Here you go, functions for writing and deleting on the SD card. Copy and paste these right into your program. Do you want to clutter up your users SD card with unnecessary files? Do you want to delete all files on your users SD cards? Do so to your hearts content! (Please don’t though, its messed up.)

// Saving a file to the SD card
public void writeFile(String filename, String data){
try {
File root = Environment.getExternalStorageDirectory();
if (root.canWrite()){
// This stores the file on the SD card
File locDataForMachines = new File(root, filename + ".txt");
FileWriter machineWriter = new FileWriter(locDataForMachines, true);
BufferedWriter machineOut = new BufferedWriter(machineWriter);
machineOut.write(data);
machineOut.close();
}
} catch (Exception e) {
// Do whatever you gotta do, maybe use some other exceptions too
}
}
// Deleting a file from the SD card, probably used in conjunction with the function above
public void deleteSDFile(String filename){
File deleteMatchingFiles = new File("/sdcard");
try{
if (deleteMatchingFiles != null){
File[] filenames = deleteMatchingFiles.listFiles();
for (File tmpf : filenames){
if (tmpf.getName().startsWith(filename)){ // can change starts with to anything you want to compare with
tmpf.delete();
}
}
}
}catch(Exception e){
// Do whatever you gotta do, maybe use some other exceptions too
}
}

Also, add this to your manifest. I hate how people never remind you what to add to the manifest. Its like they want you to fail.


<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”>

Post below and let me know how bad you think this implementation is, or let me know how it helped you. Thanks!

Advertisements

addContentView example simplified

Welcome to inphamousdevelopment.com! You were probably directed here from my portfolio site, inphamous.com, or by some miracle you were looking for some android code on Google and you happened to stumble here. Basically this site is a way I’m contributing back to the internet by sharing the code I’ve created and found to help your android producing experience easier.

Today, for a first post, I will make a simple yet demanded contribution. First, let me type out all of the searches I made before I got close to my answer

addContentView help, addContentView Android, addContentView Code Android, Adding a xml layout onto a canvas android, how to inflate a view with an xml file, multiple layout xml android, etc, etc.

The list goes on, next time I will actually better document what I searched for, so that hopefully those same searches will direct you here! In any case, by reading those searches, maybe you can see what I wanted to do. I want to draw a couple of buttons from an xml layout onto a canvas. This solution will work with drawing any xml layout onto anything else however.

LayoutInflater inflater = getLayoutInflater();
View tmpView;
tmpView = inflater.inflate(R.layout.extra_layout, null);
getWindow().addContentView(tmpView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));

Alternatively, if you want the code to be a little slimmer, just put the inflater return directly into the addcontent function, as so

LayoutInflater inflater = getLayoutInflater();
getWindow().addContentView(inflater.inflate(R.layout.extra_layout, null), new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT));

This code goes into your onCreate(Bundle whateever), preferably right underneath where you just called “setContentView(R.layout.main);”

If you found this helpful, leave a comment! I make no claim that this is the best implementation for this idea, but it works!

Buttons xml overlay

 

-Kevin Grant