Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get app's storage path without context

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?

like image 239
tekina Avatar asked Apr 09 '15 19:04

tekina


2 Answers

Service itself is a context http://developer.android.com/reference/android/app/Service.html you can use.

this.getExternalFilesDir()

on the service.

like image 124
Cristian Gomez Avatar answered Nov 15 '22 10:11

Cristian Gomez


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.

like image 33
Heshan Sandeepa Avatar answered Nov 15 '22 10:11

Heshan Sandeepa