Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/234.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用Android SDK从文件中读取一行_Android_File - Fatal编程技术网

使用Android SDK从文件中读取一行

使用Android SDK从文件中读取一行,android,file,Android,File,我已经试了三天了,我无法让我的程序从这个或任何其他文件中读取。请帮忙!我已经尝试创建另一个项目,并把这也没有工作 TextView orangeTitle; String line; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_gameplay

我已经试了三天了,我无法让我的程序从这个或任何其他文件中读取。请帮忙!我已经尝试创建另一个项目,并把这也没有工作

    TextView orangeTitle;
    String line;

@Override
protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_gameplay);
    // Show the Up button in the action bar.
    setupActionBar();
    try {
        cardReader(null);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    setOrange();
}

public String cardReader(String args[]) throws IOException
{
    FileInputStream fis = new FileInputStream("C:\\Users\\Alex\\Documents\\Android Development\\HumorMe\\assets\\orangecards.txt");
    BufferedReader bfr = new BufferedReader(new InputStreamReader(fis));
    line = bfr.readLine();
    bfr.close();
    return line;
}

public void setOrange() 
{
    orangeTitle = (TextView)findViewById(R.id.textView1);
    orangeTitle.setText(line);
}

看起来您正在尝试从计算机读取文件,这是不可能的,因为您的代码运行在单独的Android设备上。尝试在设备的SD卡上放置一个文本文件,并运行如下操作:

FileInputStream fis = new FileInputStream(Environment.getExternalStorageDirectory()+"/test.txt");

您无法通过电脑上的资产路径访问资产。有关如何访问资产文件的信息,请参阅和。例如:

InputStream is = getAssets().open("orangecards.txt")

您正在读取存储在windows分区中的文件。你想做什么?哇,真不敢相信我花了那么多时间,这是一个简单的解决方案。不过这很好用!谢谢你的帮助,尼尔!