18223

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

Question:

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!

Answer1:

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 <strong><em>myFolderForBothApplications</em></strong> and <strong><em>myFileNameForBothApplications</em></strong> can be replaced by your folder/filename, but this needs to be the same name in both applications.

<strong><em>Environment.getExternalStorageDirectory()</em></strong> 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 <a href="https://stackoverflow.com/a/22092971/5045290" rel="nofollow">this answer</a>.

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

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

<em>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().</em>

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

Answer2:

You can save the text file from your assets folder to anywhere in the sdcard, then you can read the file from the other application.

This method uses the getExternalFilesDir, that returns the absolute path to the directory on the primary shared/external storage device where the application can place persistent files it owns. These files are internal to the applications, and not typically visible to the user as media.

private void copyAssets() { AssetManager assetManager = getAssets(); String[] files = null; try { files = assetManager.list(""); } catch (IOException e) { Log.e("tag", "Failed to get asset file list.", e); } if (files != null) for (String filename : files) { InputStream in = null; OutputStream out = null; try { in = assetManager.open(filename); File outFile = new File(Environment.getExternalStorageDirectory(), filename); out = new FileOutputStream(outFile); copyFile(in, out); } catch(IOException e) { Log.e("tag", "Failed to copy asset file: " + filename, e); } finally { if (in != null) { try { in.close(); } catch (IOException e) { // NOOP } } if (out != null) { try { out.close(); } catch (IOException e) { // NOOP } } } } } private void copyFile(InputStream in, OutputStream out) throws IOException { byte[] buffer = new byte[1024]; int read; while((read = in.read(buffer)) != -1){ out.write(buffer, 0, read); } }

And to read:

File dir = Environment.getExternalStorageDirectory(); File yourFile = new File(dir, "path/to/the/file/inside/the/sdcard.ext");

Recommend

  • Bash script to read EXIF, rename JPG files and check output if file exit -> create newfile-01.jpg
  • WebGL: Access buffer from shader
  • Find files in current directory sorted by modified time and store result in an array
  • Files in gitignore
  • What is Closure Compiler?
  • How to use SBT with multiple sub project web applications?
  • Flex/AS3 very strange simple Number operation issue
  • Create a multiple screen android application
  • Facebook Messenger Platform - Detect user typing
  • Cassandra NoClassDefFoundError: com/google/common/util/concurrent/AsyncFunction
  • Scala: Function returning an unknown type
  • Unable to click on the next page button containing “>” sign
  • SPARQL date range
  • Less Conflicting Session Manager for Zope 2
  • Changing Jupyter Notebook start up folder by modifying “start in” not working any more
  • Updating Dojo provide
  • Yii2: Finding file and getting path in a directory tree
  • GAE: Way to get reference to an HttpSession from its ID?
  • List images(01.png) and descriptions(01.txt) from directory
  • Does Mobilefirst provide a provision to access web services directly?
  • how to upload multiple files in c# windows application
  • Copy to all folders batch file?
  • Caching attributes in superclass
  • print() is showing quotation marks in results
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • How to test if a URL from an Eclipse bundle is a directory?
  • Is it possible to access block's scope in method?
  • Android fill_parent issue
  • SignalR .NET Client Invoke throws an exception
  • Installing Hadoop, Java Exception about illegal characters at index 7?
  • Different response to non-authenticated users and AJAX calls
  • Get object from AWS S3 as a stream
  • How do you troubleshoot character encoding problems?
  • WPF Applying a trigger on binding failure
  • Angular 2 constructor injection vs direct access
  • Java static initializers and reflection
  • Android Google Maps API OnLocationChanged only called once
  • How can I remove ASP.NET Designer.cs files?
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal