Devraj
Devraj

Reputation: 1531

file.delete() is not working

In my application, I am calling a Service which performs the data-Uploading to the server and after successfully upload, I want to delete that uploaded file as if I don't delete that, it will upload the same file as many times service runs.

To delete the uploaded file, I am using this method:

String path = fileToBeUploaded.getPath();
File file = new File(path);
boolean delStatus= file.delete();

but it is not working. Please let me know what mistake I am doing here.

Here is my complete service class:

 public class UploadSurveyService extends Service implements SilentUploadSurveyListener{

   private static FileCache fileCache;
   private static boolean IS_REFRESHING_DATA = false;
   private static boolean NEXT_LAUNCH_SET = false;
   private  static boolean isFirstTime = true;

   private UploadSurveyAsyncTask updateLocationAsyncTask;
   private File fileToBeUploaded;

   public UploadSurveyService() {
   }

   @Override
   public IBinder onBind(Intent intent) {
     // TODO: Return the communication channel to the service.
     return null;
   }

   @Override
   public void onCreate() {
     super.onCreate();
     if(fileCache == null)
       fileCache = new FileCache(this);

     System.out.println("upload survey service created");
   }

   @Override
   public int onStartCommand(Intent intent, int flags, int startId) {
     System.out.println("upload service on start command");
     if(isFirstTime && !NEXT_LAUNCH_SET){
       isFirstTime=false;
       scheduleNextLaunch();
     }
     else if(!IS_REFRESHING_DATA && intent.hasExtra("FROM_ALARM")){
       if (Methods.checkInternetConnection(getApplicationContext(), false)) {
         IS_REFRESHING_DATA = true;

         System.out.println("service is checking the condition");

         fileToBeUploaded=  fileCache.getFileForUploading();

         if(fileToBeUploaded==null || !fileToBeUploaded.exists()){
           onUploadSurvey(false, null);
         } else {

           IS_REFRESHING_DATA=true;
           updateLocationAsyncTask = new UploadSurveyAsyncTask(getApplicationContext(),fileToBeUploaded, this);
           updateLocationAsyncTask.execute();
           Log.d("SK SERVICE", "executing upload survey task");
         }
       }
     } else {
        Log.d("SF SERVICE", "no internet");
        onUploadSurvey(false, null);
     }
     return super.onStartCommand(intent, flags, startId);
   }

   private void scheduleNextLaunch(){

     Calendar futureCalendar = Calendar.getInstance();
     Calendar currentCalendar = Calendar.getInstance();
     futureCalendar.set(Calendar.MINUTE,currentCalendar.get(Calendar.MINUTE) + 5);
     futureCalendar.set(Calendar.SECOND, 0);
     futureCalendar.set(Calendar.MILLISECOND, 0);
     //futureCalendar.set(Calendar.HOUR_OF_DAY,currentCalendar.get(Calendar.HOUR_OF_DAY)+1);

     AlarmManager alarm = (AlarmManager)getSystemService(ALARM_SERVICE);
     Intent pendingIntent= new Intent(this, UploadSurveyService.class);
     pendingIntent.putExtra("FROM_ALARM", true);
     alarm.set( alarm.RTC_WAKEUP,futureCalendar.getTimeInMillis(),PendingIntent.getService(this, 0, pendingIntent, 0));
     NEXT_LAUNCH_SET = true;
     Log.d("SF Silent Upload", "repeat set ok");
   }

   @Override
   public void onUploadSurvey(boolean status, String message) {

     updateLocationAsyncTask = null;

     IS_REFRESHING_DATA = false;

     Log.d("SF SERVICE", "after finish async task");
     scheduleNextLaunch();

     if(status) {
       String path = fileToBeUploaded.getPath();
       File file = new File(path);
       boolean delStatus= file.delete();
       Toast.makeText(UploadSurveyService.this, "file deleted" + delStatus, Toast.LENGTH_SHORT).show();
     }
   }
 }

I also have declared the permission in manifest:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 1

Views: 3810

Answers (3)

Arkady
Arkady

Reputation: 664

In my case, I've tried to delete file on ExternalStorage (aka sdcard), and it does not work.

So I've moved the file to internal storage, that you can get from context.getFilesDir() and it works.

I think that the problem in my case, is that I test it while adb and internal storage is mounted on my pc, so the system has partial access

Upvotes: 0

Todor Kostov
Todor Kostov

Reputation: 1839

Why don't you just delete fileToBeUploaded? I assume that is the file you want to delete, right? There is no need to create a new File object.

fileToBeUploaded.delete();

Upvotes: 0

AhmadReza Payan
AhmadReza Payan

Reputation: 2281

Define the WRITE_EXTERNAL_STORAGE in your manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Upvotes: 1

Related Questions