1

可能重复:
从 Android 中的资产文件夹中读取文件时出现问题

嘿,我正在尝试从 android 的 assets 文件夹中读取文件,这就是我目前所拥有的

public class TestingMusicDAO {

private static final String TAG_NAME = "MUSIC_TESTING_DAO";
private static List<Song> songs;
private ContentResolver contentResolver;
private static Context testingcontext;
private File fFile;
InputStream inputStream = null;


public TestingMusicDAO( Context context) throws IOException{
    Log.d(TAG_NAME, "Setting up testing songs");
    contentResolver = context.getContentResolver();
    testingcontext = context;
    getAllSongsFromFile();

}

public static void getAllSongsFromFile() throws IOException{
    Log.d( TAG_NAME, "Tryign to Get all Songs" );
    InputStream is;

    is = testingcontext.getAssets().open("testing");
    Log.d( TAG_NAME, "Did that work?" );
    BufferedReader bufferReader = new BufferedReader(new InputStreamReader(is));
    String inputLine;
    while((inputLine = bufferReader.readLine()) != null){
        processLine(inputLine);
    }
    bufferReader.close();

}

private static void logSongs() {
    for(Song song : songs)
        Log.d( TAG_NAME, song.toString() );
}

public List<Song> getAllSongs() {
    return songs;
}

public static void processLine(String aLine) {
    Scanner scanner = new Scanner(aLine);
    scanner.useDelimiter("=");
    if(scanner.hasNext()){
        String title = scanner.next();
        String artist = scanner.next();
        String album = scanner.next();
        String id = scanner.next();
        String albumId = scanner.next();
        String trackOrder = scanner.next();
        Log.d(TAG_NAME, "Title = " + title + "Artist = " + artist + "Album = " + album + "ID = " + id + "AlbumID = " + albumId);
    }
    else {
        Log.d(TAG_NAME, "Empty or invalid line. Unable to process");
    }


}

}

所以我基本上调用了 TestingMusicDAO 构造函数,从那里我想单独读取文件的每一行以便能够解析它们,但它一直给我一个 FileNotFoundExecption: testing。任何想法都会非常感谢!

彼得,

4

1 回答 1

1

假设异常来自该行,那么您的文件夹的根目录中is = testingcontext.getAssets().open("testing");没有命名文件。testingassets/

于 2013-01-23T23:13:41.427 回答