我正在尝试简单检查文件是否存在.我在这里看到了类似的问题,但他们没有帮助.当我运行我的应用程序时,应用程序崩溃,我得到消息“不幸的是,fileCheck1已经停止”.我在模拟器和智能手机上都出现了这个错误.
我的代码:
package com.example.fileCheck1;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.widget.TextView;
import java.io.File;
public class MyActivity extends Activity {
TextView msgText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
msgText = (TextView) findViewById(R.id.textView);
String Path = Environment.getExternalStorageDirectory().getPath()+"/ping.xml";
File file = getBaseContext().getFileStreamPath(Path);
if(file.exists()){
msgText.setText("Found");
}
if(!file.exists()){
msgText.setText("Not Found");
}
}
}
在我的清单中这样的权限:
提前致谢.