I’m developing 2 Android applications. App 1 receives the media files from app 2 which is actually a service. App 2 stores some videos and images downloaded from the internet and store them in cache directory. My goal is to find a way for app 1 to access those files. I’m currently implementing FileProvider inside the service class in app 2 to archive the goal. However, I keep facing this error in logcat:
Error sharing file: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
android.util.AndroidRuntimeException: Calling startActivity() from outside of an Activity context requires the FLAG_ACTIVITY_NEW_TASK flag. Is this really what you want?
at android.app.ContextImpl.startActivity(ContextImpl.java:912)
at android.app.ContextImpl.startActivity(ContextImpl.java:888)
at android.content.ContextWrapper.startActivity(ContextWrapper.java:379)
at com.myapp2.service.CoreService.shareFile(CoreService.java:242)
at com.myapp2.service.CoreService.execute(CoreService.java:218)
at com.myapp2.service.CoreService.access$200(CoreService.java:39)
at com.myapp2.service.CoreService$2.onReceive(CoreService.java:94)
at android.app.LoadedApk$ReceiverDispatcher$Args.lambda$getRunnable$0(LoadedApk.java:1391)
at android.app.-$$Lambda$LoadedApk$ReceiverDispatcher$Args$_BumDX2UKsnxLVrE6UJsJZkotuA.run(Unknown Source:2)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
My CoreService
looks like this:
public class CoreService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
new BroadcastReceiver() {
...
// Receive an intent from app 1 as a signal to start sharing file
shareFile(theFile);
}
}
private void shareFile(File file) {
try {
Uri fileUri = FileProvider.getUriForFile(
this,
getPackageName() + ".provider",
file
);
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("video/mp4");
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(shareIntent);
} catch (Exception e) {
Log.e(TAG, "Error sharing file: " + e.getMessage(), e);
}
}
}
The flag FLAG_ACTIVITY_NEW_TASK
does not seem having any effect. Is there any chance this happened because I’m calling startActivity from a service context instead of an activity context?
In another attempt, I did try to save those media files to the Download folder on external storage. That solved the above problem, but it leads to another one. My app 2 sometimes need to clear or move some files. This time, on android 11, permissions prevent my app 2 from accessing the files. I’m sure that the permissions READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE was granted.
Is there another way to archive my goal, sharing media files from app 2 service to app 1?