file.delete()不起作用

时间:2016-07-08 07:28:58

标签: android file service delete-file

在我的应用程序中,我正在调用Service来执行 data-Uploading到服务器,成功上传后,我想 删除上传的文件,好像我没有删除它,它会上传 与服务运行次数相同的文件。

要删除上传的文件,我使用的方法是:

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

但它不起作用。请让我知道我在这里做的错误。

这是我的完整服务类:

 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();
     }
   }
 }
  

我还在清单中声明了权限:

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

3 个答案:

答案 0 :(得分:1)

在清单中定义WRITE_EXTERNAL_STORAGE

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

答案 1 :(得分:0)

为什么不删除 fileToBeUploaded ?我假设那是你要删除的文件,对吧?无需创建新的文件对象。

fileToBeUploaded.delete();

答案 2 :(得分:0)

就我而言,我试图删除ExternalStorage(又名sdcard)上的文件,但它不起作用。

因此,我已将文件移至内部存储,可以从context.getFilesDir()获取该文件,并且可以正常工作。

我认为我的问题是,我在PC上安装了adb和内部存储时进行了测试,因此系统具有部分访问权限