How to Delete File in Android

In this tutorial how to delete the file in an Android Application is shown.

To get free space or total space of memory, we can call getFreeSpace() method or getTotalSpace() method.

The system does not guarantee that you can write as many bytes as are indicated by getFreeSpace() method.

To delete the File we have two methods:

  1. delete() method
  2. deleteFile() method

For ex.

  • myFile.delete();
  • myContext.deleteFile(filename);

Point to Remember:

  • If you want to see whether the File has been deleted or not follow the below mentioned steps:
    1. Click the DDMS button on the top
    2. Open the File Explorer
    3. Open Data folder
    4. Again open the Data folder
    5. Open your package name folder, for ex. com.example.demo
    6. Open ‘files’ folder
    7. You will not find the deleted file’s filename inside that folder

Code:

activity_main.xml

<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:paddingBottom=”@dimen/activity_vertical_margin”
android:paddingLeft=”@dimen/activity_horizontal_margin”
android:paddingRight=”@dimen/activity_horizontal_margin”
android:paddingTop=”@dimen/activity_vertical_margin”
tools:context=”.MainActivity” >

<TextView
android:id=”@+id/fdt2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”69dp”
android:text=”File I/O Operations” />

<Button
android:id=”@+id/delete1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignRight=”@+id/create1″
android:layout_below=”@+id/view1″
android:layout_marginTop=”30dp”
android:onClick=”onFileDelete”
android:text=”Delete File” />

</RelativeLayout>

MainActivity.java

package com.example.demo;

import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity{

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

public void onFileDelete(View v)
{
Intent in=new Intent(MainActivity.this,delete.class);
startActivity(in);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}

res/layout/delete_layout.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
android:layout_width=”match_parent”
android:layout_height=”match_parent” >

<EditText
android:id=”@+id/fnm3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignParentTop=”true”
android:layout_centerHorizontal=”true”
android:layout_marginTop=”72dp”
android:ems=”10″
android:hint=”Enter the File Name” >

<requestFocus />
</EditText>

<Button
android:id=”@+id/del1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignLeft=”@+id/fnm3″
android:layout_centerVertical=”true”
android:onClick=”deletef”
android:text=”Delete File” />

<Button
android:id=”@+id/back3″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_alignBottom=”@+id/del1″
android:layout_alignRight=”@+id/fnm3″
android:onClick=”back”
android:text=”Back” />

</RelativeLayout>

src/com.package.demo/delete.java

package com.example.demo;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class delete extends Activity{
File file;
EditText fnm;
String fname;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete_layout);
fnm=(EditText)findViewById(R.id.fnm3);
}

public void deletef(View v) throws Exception
{
fname=fnm.getText().toString();
try{
File dir=getFilesDir();
file=new File(dir,fname);
boolean deleted=file.delete();
if(deleted)
{
Toast.makeText(getBaseContext(), “File is successfully deleted”, Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(getBaseContext(), “File deletion is unsuccessful”, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e)
{
e.printStackTrace();
}
}

public void back(View v)
{
Intent in=new Intent(this,MainActivity.class);
startActivity(in);
}

}

AndroidManifest.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.demo”
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”17″ />

<application
android:allowBackup=”true”
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”com.example.demo.MainActivity”
android:label=”@string/app_name” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
<activity android:name=”.delete”></activity>
</application>

</manifest>

https://youtu.be/Hyyypjpl4sE

Add a Comment

Your email address will not be published. Required fields are marked *