In my application, I create a service that aims to read something from sd card.
The service is created and started at boot time.
The problem is that although I am pretty sure that the directory exists, at the boot time, the service cannot find the directory.
public void onStart(Intent intent, int startId) {
super.onStart(intent, startId);
Toast.makeText(this, "Service Started", Toast.LENGTH_LONG).show();
Context context=getBaseContext();
File sdDir = new File(Environment.getExternalStorageDirectory()+"/temp/Data/");
if(!sdDir.exists()){
sdDir.mkdir();
Toast.makeText(this, "CAN'T FIND!", Toast.LENGTH_LONG).show();
}
}
This snippet above outputs
- Service Started
- CAN’T FIND!
At first, I thought that sd card might not be mounted at boot time and that’s why the service can’t find the directory. I am still not sure about that.
Anybody has an idea? What might be the problem?
Some devices take time to mount the SD card. It may not be available immediately after Boot time.
Just poll every few seconds until it becomes available.
Also try this:
From Here