0

I have been successfully able to create files in my apps local directory using the below code

    File appDir = new File(this.getExternalFilesDir(null), "my_folder");
    if (!appDir.exists())
    {
        boolean result = appDir.mkdir();
        if (!result) {
            Log.i(Util.TAG, LOG_LABEL
                    + "::  Unable to create \"my_folder\" Directory : "
                    + appDir.getAbsolutePath()
                    + "  Directory already exists !");
        }
    }


    File HTMLFile = new File(appDir,"html.txt");

But now when I tried to do the same from a service, the file was not being created, I even checked using 'HTMLFile.exists()' and it says the file does not exist.

My question is, Is it actually possible to create files from a service? or am I missing something here.

Code_Yoga
  • 2,968
  • 6
  • 30
  • 49

1 Answers1

0

It is possible to write to the storage of your android: by the help of the following methods

  1. Make sure you provide uses permission to write and read from storage in manifest file.

  2. I have used this code:

    private File newFile;
    
    public ListenNotification() {
            Log.d("Service","InConstrutor");
    newFile=newFile(Environment.getExternalStorageDirectory(),"NotificationFromService");
    
            if(!newFile.exists()){
                Log.d("Service","Created");
                newFile.mkdir();
            }
            else{
                Log.d("Service","Existed");
            }
        }
    File HTMLFile = new File(newFile,"html.txt");
    
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
beast98
  • 31
  • 3