【问题标题】:How to access /data folder of another application through your application?如何通过您的应用程序访问另一个应用程序的 /data 文件夹?
【发布时间】:2016-04-18 16:48:24
【问题描述】:

应用程序生成了一个文本文件,我想获取该文件并将其作为字符串读取到我的应用程序中。我怎样才能做到这一点,任何帮助将不胜感激。这两个应用程序都是我的应用程序,因此我可以获得权限。

谢谢!

【问题讨论】:

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


    【解决方案1】:

    您可以将资产文件夹中的文本文件保存到 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");
    

    【讨论】:

    • 我可以从内部存储而不是从 sd 卡中获取文件吗?抱歉,我是应用程序 android 世界的新手。非常感谢您的帮助:) 另外,例如,文件位于目录 /data/angrybirds 中(我只是举例,我可以访问其他文件数据),那么我将如何处理。请对您的代码进行一些解释。再次感谢:)
    • 谢谢你,如果我在某个地方卡住了,我会再次打扰你:)。非常感谢我很抱歉,但我是一个新程序员
    【解决方案2】:

    使用标准的 android-storage 可以做到这一点,所有用户的文件也都存储在其中:

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

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

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

    Environment.getExternalStorageDirectory() 将文件对象返回到设备的通用、可用文件目录,用户也可以看到相同的文件夹。 通过调用 getPath() 方法,将返回一个表示此存储路径的字符串,因此您可以在之后添加您的文件夹/文件名。

    所以一个完整的代码示例应该是:

    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() 确定其当前状态。

    我不知道这是否是解决您的问题的最佳/最安全的方法,但它应该可以工作。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-23
      • 1970-01-01
      • 2012-12-14
      相关资源
      最近更新 更多