Saturday, June 6, 2015

// //

Delete the latest file from any specific folder in sdcard in android

There so many people who still confuse about deleting the specific files in specific folder in sdcard. Now i want to show you how to do it in a very simple way..
1.

delete file from sdcard folder

 2.
delete file from sdcard folder
Asking confirmation.

3.
delete file from sdcard folder
File successfully deleted and the next file will show



activity_main.xml :
 Here i have textview we will used it to display the files inside the specific folder.


   

     


MainActivity.java
import java.io.File;

import android.support.v7.app.ActionBarActivity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;


public class MainActivity extends ActionBarActivity {

 Button delete;
 TextView filestv;
 String name ;
 String path="/sdcard/media/alarms/";
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        delete = (Button) findViewById(R.id.delete);
        filestv  = (TextView) findViewById(R.id.textview);
        
        boolean exists = (new File(path)).exists();  
   if (!exists){new File(path).mkdirs();}
  namebut();
  
  delete.setOnClickListener(new Button.OnClickListener(){

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
  
     if (delete.getText().toString().equalsIgnoreCase("No Files")) {
         
         //   open();  
           
     } else {
      open(); 
    //
     
     }
   }});    
}

public void namebut(){
 
 String root_sd = Environment.getExternalStorageDirectory().toString();
     File file = new File(path) ;       
      File list[] = file.listFiles();
        for(File f:list)
          {
         name =  file.getName();
        filestv.setText(f.getName());
        //add new files name in the list
       //  delete.setText(name );
  
          
          }  
  
    
    }



public void open(){
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
    alertDialogBuilder.setMessage(R.string.decision);
    alertDialogBuilder.setPositiveButton(R.string.positive_button, 
    new DialogInterface.OnClickListener() {
  
       @Override
       public void onClick(DialogInterface arg0, int arg1) {
       // String editText1 = e1.getText().toString();
      //  delete.setText(name); 
        String name = (String) filestv.getText();
     
     filestv.setText(name);
        
        File pathrec = new File(path + name);
      
        if (pathrec != null && pathrec.exists()) {
              deleteRecording();
              namebut();
           //   mp.stop();
    // mp.release();
            //  delete.setText("File Deleted" );
          }else{
          // filenotfound();
           //Toast.makeText(getApplicationContext(), "Not found "+pathrec, Toast.LENGTH_LONG).show(); 
            filestv.setText("No files found" );
           // namebut();
           //intent();
          //delete.setText(path+editText1 );
           
          }
   
       }
    });
    alertDialogBuilder.setNegativeButton(R.string.negative_button, 
    new DialogInterface.OnClickListener() {
   
       @Override
       public void onClick(DialogInterface dialog, int which) {
  //        Intent negativeActivity = new Intent(getApplicationContext(),com.example.alertdialog.NegativeActivity.class);
      //    startActivity(negativeActivity);
   }
    });
     
    AlertDialog alertDialog = alertDialogBuilder.create();
    alertDialog.show();
     
 }


private void deleteRecording() {
 // String editText1 = e1.getText().toString();
// Button delete = (Button)findViewById(R.id.delete);
  
 String name = (String) filestv.getText();
 
  filestv.setText(name);
 
  File pathrec = new File(path + name);
    if (pathrec != null && pathrec.exists()) {
      
     pathrec.delete();
      namebut();
    // Toast.makeText(this, "file deleted", Toast.LENGTH_SHORT).show();
     
    }else{
 Toast.makeText(this, "No file to delete", Toast.LENGTH_SHORT).show();
    }

}





}
And add this permission to your Android Manifest:
 
    
     
POINT TO NOTE: This code works on how to display the files inside the folder into the textview(filestv)
String root_sd = Environment.getExternalStorageDirectory().toString();
     File file = new File(path) ;       
      File list[] = file.listFiles();
        for(File f:list)
          {
         name =  file.getName();
        filestv.setText(f.getName());
        //add new files name in the list
       //  delete.setText(name );
  
          
          }  
You may also like: How to set recorded audio as an alarm tone through Android code