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

五迷三道 提交于 2020-01-03 17:19:28

问题


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!


回答1:


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.




回答2:


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");


来源:https://stackoverflow.com/questions/34771313/how-to-access-data-folder-of-another-application-through-your-application

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!