I'm trying to make a simple check if the file exist. I saw similar questions here, but they didn't help. When I run my application, the app crashes and I got message "Unfortunatelly, fileCheck1 has stopped". I got this error both on emulator and smartphone.
My code:
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");
}
}
}
In my Manifest such permissions:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
Thanks in advance.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…