如何通过您的应用程序访问另一个应用程序的/ data文件夹?

时间:2016-01-13 16:00:08

标签: java android android-studio file-permissions android-permissions

有一个应用程序生成的文本文件,我想将该文件作为我的应用程序中的字符串读取。我怎样才能实现这一点,任何帮助都会感激不尽。这两个应用程序都是我的应用程序,所以我可以获得权限。

谢谢!

2 个答案:

答案 0 :(得分:3)

这可以使用标准的android-storage,其中也存储了所有用户的文件:

您需要做的就是在两个应用程序中访问相同的文件和相同的路径,例如:

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

myFolderForBothApplications myFileNameForBothApplications 可以替换为您的文件夹/文件名,但这必须是两个应用程序中的名称相同。

Environment.getExternalStorageDirectory() 将File-Object返回到设备的公共可用文件目录,该文件目录也是用户可以看到的文件夹。 通过调用getPath()方法,将返回表示此存储路径的String,因此您可以在之后添加文件夹/文件名。

所以完整的代码示例是:

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;
}

之后,您可以在文件中写入或从文件中读取,例如在this answer

请注意,这样存储的文件对用户可见,我可以由用户编辑/删除。

还要注意getExternalStorageDirectory()的JavaDoc是什么:

返回主外部存储目录。如果用户已在其计算机上安装此目录,已从设备中删除或发生其他一些问题,则该目录当前可能无法访问。您可以使用getExternalStorageState()确定其当前状态。

我不知道这是否是解决问题的最佳/最安全的方法,但它应该有效。

答案 1 :(得分:2)

您可以将资源文件夹中的文本文件保存到SD卡中的任何位置,然后就可以从其他应用程序中读取该文件。

此方法使用getExternalFilesDir,它返回主应用程序可以放置其拥有的永久文件的主共享/外部存储设备上的目录的绝对路径。这些文件是应用程序的内部文件,通常不会被用户视为媒体。

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

并阅读:

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