2016-07-08 6 views
1

작동하지 않습니다, 여러 번 서비스 실행과 동일한 파일을 업로드합니다.File.delete를()는 내가 그것을 삭제하지 않는 경우 등이 업로드 된 파일을 삭제 원하는, 내가 서버에 <code>data-Uploading</code> 성공적 후 업로드를 수행하는 <code>Service</code>를 호출하고, 내 응용 프로그램에서

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

허가를 선언 했습니까? –

+0

@ Er.Arjunsaini 어느 허락? – Devraj

+0

사용 권한 android : name = "android.permission.WRITE_EXTERNAL_STORAGE" –

답변

1

가 매니페스트에 WRITE_EXTERNAL_STORAGE를 정의

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

Lolz, 형제, 나는 내 파일이 서버에 업로드되는 이유 인이 사용 권한을 사용하고 있습니다. 파일을 읽지 않고도이 파일을 읽을 수 없으며 모든 파일을 읽을 수 없다고 생각합니다. – Devraj

+0

어쩌면 파일 전달이 잘못되었을 수도 있습니다.'file.exists() '에 의해 파일의 존재를 확인하십시오. –

0

fileToBeUploaded을 삭제하지 않으시겠습니까? 내가 그 파일을 삭제하고 싶다고 가정합니다. 새 파일 개체를 만들 필요가 없습니다.

fileToBeUploaded.delete(); 
+0

감사합니다. Todor,하지만 그 역시 노력하지 않았습니다. :( – Devraj