I want to get app directory(and some special pre built sub directory) of my app in dobackground of my asyncTask. I am using asynctask for sending an email. and my email asynctask class is not in an activity. I want to get an certain file in app data directory and attaching it for the sending. in this section I have not any context for getting the path(data directory). Goal is getting my file path in dobackground of my asynctask when my asynctask class is in a separate file(not inside of an activity)
Asked
Active
Viewed 135 times
2 Answers
2
I guess you want to pass parameters to asynctasks. This will probably help you.

solaza
- 1,251
- 1
- 17
- 30
-
My dear friend : The problem is simple and clear. The app has a file is located in a subdirectory of the app directory (or in the app directory). Nobody has access to this location except the app. I want to get this path when I have not any context(when we are in asynctask class). Is any way for getting the app directory without using the context. when we need the path , we have not any context. You suppose we want to get app directory in DoInBackground method of an asynctask. – maniaq Jul 09 '19 at 08:33
-
In my doInBackground method I need the file path for passing to my email class(for specifying of file location for attaching). But in my doInBackground method I cannot get the app directory. – maniaq Jul 09 '19 at 08:40
-
Now I see what you actually want. I use this to get path You can't get this without context but you can pass it as parameter and use it if this helps. fun getInternalStorage(cont: Context?): String { return cont?.filesDir.toString() } – solaza Jul 09 '19 at 11:16
-
Yes I think passing of string path is the best solution. I think passing the context and using it in doInBackgrond is not complete safe and if the context does not exist at that time , our process is not done. I prefer string path passing.Thanks – maniaq Jul 09 '19 at 15:58
1
Simplified example:
public class AsyncTaskMy extends AsyncTask<Void, Void, Void> {
private WeakReference<Context> contextRef;
public AsyncTaskMy(Context context) {
contextRef = new WeakReference<>(context);
}
@Override
protected Void doInBackground(Void... voids) {
Context context = contextRef.get();
if (context != null) {
try {
PackageManager m = context.getPackageManager();
String s = context.getPackageName();
PackageInfo p = m.getPackageInfo(s, 0);
s = Environment.getExternalStorageDirectory().toString();
s += p.applicationInfo.dataDir;
sendFileWithEmail(new File(s));//or whatever you want
} catch (PackageManager.NameNotFoundException e) {
e.printStackTrace();
}
return null;
}
return null;
}
MainActivity.java
class MainActivity extends AppCompatActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AsyncTaskMy asyncTaskMy = new AsyncTaskMy(this);
asyncTaskMy.execute();
}
}

Daniel Jorn
- 176
- 3
- 10
-
In any case I didn't understand the question so I appreciate if you would give me a response or update your question with minimal amount of code – Daniel Jorn Jul 09 '19 at 07:18
-
Thanks. But I want to get my app directory. If I could find my app directory in the phone then I know the rest path for reaching to my special file. In your code we could access to download director ! – maniaq Jul 09 '19 at 07:49
-
Mmm, I think the best way this to use an intenal memory (see differences between internal\external memory (https://stackoverflow.com/questions/5092591/what-are-the-differences-among-internal-storage-external-storage-sd-card-and-r)). Using the internal one good when user of an app should not to have access to the downloaded file (but you still can use this file in your app) and external memory - better choise for letting users interact with the file – Daniel Jorn Jul 09 '19 at 07:55
-
How do you downloading file to your directory? Could you give some code? – Daniel Jorn Jul 09 '19 at 08:10
-
Sorry, I just did not understand the question well. You need to get path without context, right? Why do use AsyncTask? – Daniel Jorn Jul 09 '19 at 10:17
-
I will try it.I hope there is no problem in existing of the context when we try use it. – maniaq Jul 09 '19 at 10:56