【问题标题】:Get the Path from a File inside file directory in Android从Android文件目录中的文件获取路径
【发布时间】:2015-10-29 06:24:27
【问题描述】:

我在私有应用文件夹的files 目录中有一个名为abikor.txt 的文件:

/data/data/myapp/files/abikor.txt

我怎样才能得到这个文件的路径?

我看到了以下问题,但它们并没有解决我的问题。

(一般情况下,如何获取私有app目录下/files目录下的文件路径?)

亲切的问候

【问题讨论】:

    标签: android file path


    【解决方案1】:

    对于内部存储路径,您需要指定类似这样的内容

    String path = context.getFilesDir().getAbsolutePath();
    

    然后从路径创建一个文件对象

    File file = new File(path + "/abikor.txt");
    

    如果你想从中读取文件,那么

    int length = (int) file.length();
    
    byte[] bytes = new byte[length];
    
    FileInputStream in = new FileInputStream(file);
    try {
        in.read(bytes);
    } finally {
        in.close();
    }
    
    String contents = new String(bytes);
    

    【讨论】:

      猜你喜欢
      • 2015-04-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-04-26
      • 2017-06-17
      • 1970-01-01
      • 2014-04-21
      • 2015-12-07
      相关资源
      最近更新 更多