gpt4 book ai didi

android - file.delete() 不工作

转载 作者:行者123 更新时间:2023-11-30 01:12:45 25 4
gpt4 key购买 nike

在我的应用程序中,我正在调用一个执行以下操作的 Servicedata-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();
}
}
}

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"/>

最佳答案

在 list 中定义 WRITE_EXTERNAL_STORAGE:

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

关于android - file.delete() 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38261150/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com