簡體   English   中英

使用ADB時,Android Studio“找不到文件”,桌面版本有效

[英]Android studio “File not found” when using ADB, Desktop version works

每當我嘗試通過ADB在手機上運行libdgx應用程序時,android studio都無法在“ android / assets”文件夾中找到文件。 但是,當我運行桌面版本時,它可以正常工作。

我正在使用它來讀取文件:

File file = new File("BlocksProgression.txt");
reader = new BufferedReader(new FileReader(file));

如前所述,當我運行桌面啟動器時,此方法工作正常,但android啟動器返回此錯誤:

W/System.err: java.io.FileNotFoundException: BlocksProgression.txt (No such file or directory)

我已經搜索了一個多小時,但是似乎找不到正確設置資產文件夾的方法。

任何幫助,將不勝感激。

好吧,當使用File file = new File("BlocksProgression.txt")您不是從資產中讀取文件,而是從默認文件目錄中讀取,從資產中讀取文件的正確方法如下

BufferedReader reader = null;
try {
    reader = new BufferedReader(
        new InputStreamReader(getAssets().open("BlocksProgression.txt"), "UTF-8")); 

    // do reading, usually loop until end of file reading 
    String mLine;
    while ((mLine = reader.readLine()) != null) {
       //process line
       ...
    }
} catch (IOException e) {
    //log the exception
} finally {
    if (reader != null) {
         try {
             reader.close();
         } catch (IOException e) {
             //log the exception
         }
    }
}

這段代碼來自這個答案

找到了答案: https : //github.com/libgdx/libgdx/wiki/File-handling#reading-from-a-file

事實證明Libgdx希望您使用FileHandle對象讀取文件。 使用我的代碼成為:

FileHandle file = Gdx.files.internal("BlocksProgression.txt");
String data = file.readString();

這只是將文本作為字符串返回。 希望這可以幫助一些人。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM