2016-03-04 84 views
0

我有一個與android設備文件目錄相關的問題。我正在嘗試使用掃描儀讀取文本文件。 line41:File f = new File(「./ sample/src /」+ category +「。txt」); line48:掃描儀sc =新的掃描儀(文件);Android設備目錄問題

但它無法找到我提供的相對路徑中的文件。這導致它拋出filenotfoundexception。有誰知道如何解決這個問題? *注意:我能夠順利運行筆記本電腦上的相對路徑,但在我的Android設備上失敗。

Codes on my android device Error

回答

0

如果您使用的是txt文件從項目比你應該把你的txt文件,在項目的入資產的文件夾。使用assest可以使用該文件。

Sometihg這樣的:

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