Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access /data folder of another application through your application?

there is a text file that an application produces, I would like to take that file and read it as strings in my application. How can I achieve that, any help would be grateful. Both applications are my applications so I can get the permissions.

Thank you!

like image 203
Abdulrehman Avatar asked Jan 13 '16 16:01

Abdulrehman


People also ask

Can an app access data from another app?

Just as an app can send data to other apps, it can also receive data from other apps as well. Think about how users interact with your application and what data types you want to receive from other applications.

How do I access application data folder?

To open the AppData folder on Windows 10, 8 & 7: Open File Explorer/Windows Explorer. Type %AppData% into the address bar and hit enter. Navigate to the required folder (Roaming or Local)

How do you locate the application data folder and how is it different from the Program files folder?

You can view the AppData folder manually by going into your Users folder, which is there in the C drive. In my case, the path is C:\Users\ADMIN . Now you should be able to see the AppData folder in your User folder. You can also access the AppData folder directly using the AppData system variable.


1 Answers

This is possible using the standard android-storage, where all the user's files are stored too:

All you need to do is to access the same file and the same path in both applications, so e.g.:

String fileName = Environment.getExternalStorageDirectory().getPath() + "myFolderForBothApplications/myFileNameForBothApplications.txt";

Where myFolderForBothApplications and myFileNameForBothApplications can be replaced by your folder/filename, but this needs to be the same name in both applications.

Environment.getExternalStorageDirectory() returns a File-Object to the common, usable file-directory of the device, the same folder the user can see too. By calling the getPath() method, a String representing the path to this storage is returned, so you can add your folder/filenames afterwards.

So a full code example would be:

String path = Environment.getExternalStorageDirectory().getPath() + "myFolderForBothApplications/";
String pathWithFile = path + "myFileNameForBothApplications.txt";

File dir = new File(path);
if(!dir.exists()) {       //If the directory is not created yet
    if(!dir.mkdirs()) {   //try to create the directories to the given path, the method returns false if the directories could not be created
        //Make some error-output here
        return;
    }
}
File file = new File(pathWithFile);
try {
    f.createNewFile();
} catch (IOException e) {
    e.printStackTrace();
    //File couldn't be created
    return;
}

Afterwards, you can write in the file or read from the file as provided e.g. in this answer.

Note that the file stored like this is visible for the user and my be edited / deleted by the user.

Also note what the JavaDoc for the getExternalStorageDirectory() says:

Return the primary external storage directory. This directory may not currently be accessible if it has been mounted by the user on their computer, has been removed from the device, or some other problem has happened. You can determine its current state with getExternalStorageState().

I do not know if this is the best/safest way to fix your problem, but it should work.

like image 195
Florian Mötz Avatar answered Sep 29 '22 07:09

Florian Mötz