Saturday, November 10, 2012

How To Delete A File In Android

Part 10: Create An Automobile Log Application

This is the last of a ten part series on how to create an automobile log application in Android. Any part of the series can be viewed as an independent session or you can view the project as a whole. In order to understand this tutorial it will be beneficial to review the last post about creating Android options menu.

1. Modify the MenuChoice method in the AutomobileLogActivity.java file. This will call the DeleteFile method once the user selects the Delete options menu. If the delete operation is successful then it the DeleteFile method will return true.

private boolean MenuChoice(MenuItem item){ switch (item.getItemId()){  case 0: {    SaveFile();    return true;  }  case 1: {    if (DeleteFile()) {    ClearViews();    Toast.makeText(this, "File has been succesfully deleted.", Toast.LENGTH_SHORT).show();    } else {    Toast.makeText(this, "Delete operation failed.", Toast.LENGTH_SHORT).show();    }    return true;  }  case 2: {    startActivityForResult(new Intent("net.htmlpress.openfiledialog"), request_Code);    return true;  }}Log.d("menu status", "false");return false;}

2. Add the DeleteFile() method. This will get the current path for internal files as it creates a file object with the path name and filename given. The f.delete method will delete the file.

private boolean DeleteFile(){ File f = new File(this.getFilesDir().getPath().toString() + '/' + GetFilename());return f.delete();}

And this concludes the tutorial on how to create an Automobile log application.

Related Posts:

Tags: android tutorial


Source : htmlpress[dot]net

0 comments:

Post a Comment