【问题标题】:Android Device Directory issueAndroid 设备目录问题
【发布时间】:2016-03-04 23:38:30
【问题描述】:

我有一个与 android 设备文件目录有关的问题。我正在尝试使用扫描仪读取文本文件。

line41: 文件 f = new File("./sample/src/"+category+".txt"); 第 48 行:扫描仪 sc = 新扫描仪(文件);

但是它无法在我提供的相对路径中找到该文件。这会导致它抛出一个 filenotfoundexception。有谁知道如何解决这个问题? *注意:我可以在我的笔记本电脑上顺利运行相对路径,但在我的安卓设备上失败了。

【问题讨论】:

    标签: java android directory


    【解决方案1】:

    如果您使用项目中的 txt 文件,则应将 txt 文件放在项目的 assest 文件夹中。使用 assest 你可以使用那个文件。

    有点像这样:

       AssetManager am = getAssets();
    InputStream inputStream = am.open("myfoldername/myfilename");
    File file = createFileFromInputStream(inputStream);
    
    private File createFileFromInputStream(InputStream inputStream) {
    
       try{
          File f = new File(my_file_name);
          OutputStream outputStream = new FileOutputStream(f);
          byte buffer[] = new byte[1024];
          int length = 0;
    
          while((length=inputStream.read(buffer)) > 0) {
            outputStream.write(buffer,0,length);
          }
    
          outputStream.close();
          inputStream.close();
    
          return f;
       }catch (IOException e) {
             //Logging exception
       }
    
       return null;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-09
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-04-16
      • 2022-01-24
      相关资源
      最近更新 更多