0

Im trying to create a file with one app, and check if it exists with another app. What im tryin to achieve is global file in cache, which can be accessed by any app on the device. I've read this: this and this one. But seems I am lost. I'm getting new file created every time I check from another app, did first app created a file.

private boolean doesFileExist() {
        File file = new File(this.getCacheDir().toString() + "myfile");
        if(file.exists()) {
            return true;
        }
        else return false;
    }

    private void createFileForMe() {
        File file = new File(this.getCacheDir().toString() + "myfile", "myfile");
        file.setReadable(true, false);
        Log.v("FILE", "created file");
    }

public void onCreate() {
        super.onCreate();
            if (!doesFileExist()) {
                Log.v("FILE", "FILE DOESnt EXIST");
                createFileForMe();
            } else Log.v("FILE", "FILE DOES EXIST");
    }
Community
  • 1
  • 1
  • 2
    "Im trying to create a file with one app, and check if it exists with another app. What im tryin to achieve is global file in cache, which can be accessed by any app on the device" -- `getCacheDir()` is not "global". Each app has its own directory returned by `getCacheDir()` on internal storage. – CommonsWare Aug 07 '14 at 20:08
  • I just tried getFilesDir() but I still get same result. –  Aug 07 '14 at 20:16
  • `getFilesDir()` is also on internal storage. – CommonsWare Aug 07 '14 at 20:17
  • Please help me. I cant find any method that is NOT for app itself. Neither for internal storage nor for external. –  Aug 07 '14 at 20:24
  • I would recommend you reconsider your whole plan ("What im tryin to achieve is global file in cache, which can be accessed by any app on the device"). Whatever problem you are trying to solve probably can be solved more securely in some other way. – CommonsWare Aug 07 '14 at 20:27
  • Checking is any duplicate of service running after you start the service isnt that easy for a rookie. [This is my problem](http://stackoverflow.com/questions/25120169/how-to-check-inside-service-is-same-service-already-running) –  Aug 07 '14 at 20:33

1 Answers1

0

Android Javadoc of the getCacheDir() says:

Returns the absolute path to the application specific cache directory on the filesystem.

It means that it's available ONLY to single app. The same method returns different location for your second application. These apps can't share cache using mentioned method.

You have to pick another location for sharing your data. Please note that most of the space of internal storage is restricted. Maybe it's better to use built in external storage (logically external, not sdcard).

Update

I see 2 options:

  • Store data on external storage in public location, you can read about it here
  • Use ContentProvider to share your private data on internal storage to the other app,you can read about it here
Damian Petla
  • 8,963
  • 5
  • 44
  • 47
  • I cant find any Contexts method that is not specific to a single app. please help me –  Aug 07 '14 at 20:21
  • Did you read this http://developer.android.com/training/basics/data-storage/files.html#WriteExternalStorage ? – Damian Petla Aug 07 '14 at 20:28