0

I have a folder in which i store images....So what i want to do is upload this images to server and once the upload is complete i want to delete this images.. This uploading and deleting should happen in background...So i have created a service that works fine..What i want is whenever a new file comes in this folder it should automatically upload..i.e the service should automatically start itself...Is there any way to do this...??

public class MyServices extends Service {

    @Override
    public IBinder onBind(Intent intent) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Started",
                Toast.LENGTH_LONG).show();

        sendfile();
        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    public void onDestroy() {
        // TODO Auto-generated method stub
        Toast.makeText(getApplicationContext(), "Service Stopped",
                Toast.LENGTH_LONG).show();
        super.onDestroy();
    }
}

the sendfile() scans the folder and sends the images....So do i have to keep on calling this method infinitely or is there any other way???

Adolf Dsilva
  • 13,092
  • 8
  • 34
  • 45
  • You can use an alarm to trigger this method every few minutes; see http://developer.android.com/reference/android/app/AlarmManager.html – Dan Jan 28 '13 at 11:55
  • Try using a FileObserver , an example: http://stackoverflow.com/questions/4571461/broadcast-receiver-wont-receive-camera-event – baboo Jan 28 '13 at 11:56

3 Answers3

0

You can use AlarmManager and sometimes run your service and check the folder. (But be aware of battery cunsuming app =) ).

dilix
  • 3,761
  • 3
  • 31
  • 55
0

Here are a few options:

  1. You could use a FileObserver to watch the directory for changes.
  2. If your app is creating the images, you could call startService each time a new image is added.
  3. You could listen for ACTION_NEW_PICTURE to know when new pictures are added (assuming the app adding the pictures behaves like the camera app).
aij
  • 5,903
  • 3
  • 37
  • 41
0

..So what i want to do is upload this images to server and once the upload is complete i want to delete this images.. This uploading and deleting should happen in background...

This Github library exactly does that - https://github.com/gotev/android-upload-service 2.3K stars Apr 2020

enter image description here

" -Easily upload files (Multipart/Binary/FTP out of the box) in the background with progress indication notification

-upload files to a server with FTP, HTTP multipart/form-data or binary requests

-handle multiple concurrent uploads in the background, even if the device is idle (Doze mode)

-automatically retry failed uploads, with a configurable exponential backoff possibility to automatically delete uploaded files when the upload is successful

Apps and libraries powered by this library-

-JIRA Cloud

-Quora

...

"

zennni
  • 1,397
  • 15
  • 12