I have a background service which activates BroadcastReceiver
class when it receives some content. I want to save that to the app's path in external dir, which is returned by calling Context.getExternalFilesDir()
(/sdcard/Android/data/com.example.app/). However, since this happens in the background, I have no context and so I cannot get that path. I absolutely do not wish to hard code the path and want the data to get deleted when the app is uninstalled. Any other way in which I can get that complete path?
Service itself is a context http://developer.android.com/reference/android/app/Service.html you can use.
this.getExternalFilesDir()
on the service.
Prior to Android 6 & prior to forced scoped storage, the following would work with Android 5.
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
This answer no longer works on Android 11 and in 2020.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With