Repository: thundernest/k-9
Issue: Crash when saving attachment because no Activity found for CREATE_DOCUMENT Intent

* I have set up an account with pgp (most messages like the one from logcat are only encrypted, not signed)

* The message text is not shown but a file to show or save is presented
* pressing save crashes 
* open only suggests openkeychain

* Version is 5.707 from f-droid
* thunderbird opens the mail inline

```
<<< log_count = 45 >>>
[03-21 10:32:12.109 914:997 I/ActivityManager]
Start proc 14095:com.fsck.k9/u0a272 for service com.fsck.k9/androidx.work.impl.background.systemjob.SystemJobService

[03-21 10:32:12.116 14095:14095 E/com.fsck.k9]
Not starting debugger since process cannot load the jdwp agent.

[03-21 10:32:13.396 914:6215 I/ActivityManager]
START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.fsck.k9/.activity.MessageList bnds=[40,1240][312,1616]} from uid 10227

[03-21 10:32:13.699 914:993 I/ActivityManager]
Displayed com.fsck.k9/.activity.MessageList: +276ms

[03-21 10:32:13.830 480:1182 W/SurfaceFlinger]
Attempting to set client state on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:32:13.830 480:1182 W/SurfaceFlinger]
Attempting to destroy on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:32:16.281 14095:14095 I/com.fsck.k9]
The ClassLoaderContext is a special shared library.

[03-21 10:32:16.351 914:997 I/ActivityManager]
Start proc 14155:com.android.webview:sandboxed_process0/u0i20 for webview_service com.fsck.k9/org.chromium.content.app.SandboxedProcessService0

[03-21 10:32:17.568 914:7800 I/ActivityManager]
START u0 {act=android.intent.action.VIEW dat=content://com.fsck.k9.tempfileprovider/temp/9ca99f367b8b669c80ba814d052dbb6d73a0c320?mime_type=application/octet-stream typ=application/octet-stream flg=0x10080001 cmp=android/com.android.internal.app.ResolverActivity} from uid 10272

[03-21 10:32:18.348 14095:14103 I/chatty]
uid=10272(com.fsck.k9) FinalizerDaemon identical 1 line

[03-21 10:32:19.640 14095:14095 E/AndroidRuntime]
FATAL EXCEPTION: main
Process: com.fsck.k9, PID: 14095
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.CREATE_DOCUMENT cat=[android.intent.category.OPENABLE] typ= (has extras) }
	at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:2007)
	at android.app.Instrumentation.execStartActivity(Instrumentation.java:1673)
	at android.app.Activity.startActivityForResult(Activity.java:4587)
	at androidx.fragment.app.FragmentActivity.startActivityForResult(FragmentActivity.java:675)
	at androidx.core.app.ActivityCompat.startActivityForResult(ActivityCompat.java:234)
	at androidx.fragment.app.FragmentActivity.startActivityFromFragment(FragmentActivity.java:795)
	at androidx.fragment.app.FragmentActivity$HostCallbacks.onStartActivityFromFragment(FragmentActivity.java:932)
	at androidx.fragment.app.Fragment.startActivityForResult(Fragment.java:1278)
	at androidx.fragment.app.Fragment.startActivityForResult(Fragment.java:1266)
	at com.fsck.k9.ui.messageview.MessageViewFragment.onSaveAttachment(MessageViewFragment.java:818)
	at com.fsck.k9.ui.messageview.AttachmentView.onSaveButtonClick(AttachmentView.java:105)
	at com.fsck.k9.ui.messageview.AttachmentView.onClick(AttachmentView.java:96)
	at android.view.View.performClick(View.java:6597)
	at android.view.View.performClickInternal(View.java:6574)
	at android.view.View.access$3100(View.java:778)
	at android.view.View$PerformClick.run(View.java:25906)
	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:6718)
	at java.lang.reflect.Method.invoke(Native Method)
	at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:491)
	at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

[03-21 10:32:19.646 914:3338 W/ActivityManager]
  Force finishing activity com.fsck.k9/.activity.MessageList

[03-21 10:32:19.654 914:995 I/ActivityManager]
Showing crash dialog for package com.fsck.k9 u0

[03-21 10:32:20.149 914:994 W/ActivityManager]
Activity pause timeout for ActivityRecord{8bc72c4 u0 com.fsck.k9/.activity.MessageList t50 f}

[03-21 10:32:21.259 914:3338 W/ActivityManager]
  Force finishing activity com.fsck.k9/.activity.MessageList

[03-21 10:32:21.263 914:3338 I/ActivityManager]
Killing 14095:com.fsck.k9/u0a272 (adj 0): crash

[03-21 10:32:21.270 480:1902 W/SurfaceFlinger]
Attempting to destroy on removed layer: 9fc3519 com.fsck.k9/com.fsck.k9.activity.MessageList#0

[03-21 10:32:21.333 914:997 I/ActivityManager]
Start proc 14216:com.fsck.k9/u0a272 for broadcast com.fsck.k9/.widget.list.MessageListWidgetProvider

[03-21 10:32:21.335 14216:14216 E/com.fsck.k9]
Not starting debugger since process cannot load the jdwp agent.

[03-21 10:32:21.454 480:1902 W/SurfaceFlinger]
Attempting to destroy on removed layer: 3a0ad17 Application Error: com.fsck.k9#0

[03-21 10:32:23.912 1434:1434 W/NotificationEntryMgr]
removeNotification for unknown key: 0|com.fsck.k9|5|null|10272

[03-21 10:32:26.428 14216:14229 I/com.fsck.k9]
Waiting for a blocking GC ProfileSaver

[03-21 10:32:26.439 14216:14229 I/com.fsck.k9]
WaitForGcToComplete blocked ProfileSaver on HeapTrim for 11.418ms

[03-21 10:32:29.961 1434:1434 W/NotificationEntryMgr]
removeNotification for unknown key: 0|com.fsck.k9|5|null|10272

[03-21 10:32:50.803 914:914 I/NotificationService]
Cannot find enqueued record for key: 0|com.fsck.k9|5|null|10272

[03-21 10:32:50.813 1434:1434 W/NotificationEntryMgr]
removeNotification for unknown key: 0|com.fsck.k9|5|null|10272

[03-21 10:32:52.834 1434:1434 W/NotificationEntryMgr]
removeNotification for unknown key: 0|com.fsck.k9|5|null|10272

[03-21 10:32:52.856 14216:14238 I/WM-WorkerWrapper]
Worker result SUCCESS for Work [ id=af96888a-282c-4d28-b868-08a0560ce31f, tags={ com.fsck.k9.job.MailSyncWorker, MailSync } ]

[03-21 10:35:54.955 914:13084 I/ActivityManager]
START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=com.fsck.k9/.activity.MessageList bnds=[40,1240][312,1616]} from uid 10227

[03-21 10:35:55.315 914:993 I/ActivityManager]
Displayed com.fsck.k9/.activity.MessageList: +339ms

[03-21 10:35:55.455 480:572 W/SurfaceFlinger]
Attempting to set client state on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:35:55.455 480:572 W/SurfaceFlinger]
Attempting to destroy on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:35:57.746 14216:14216 I/com.fsck.k9]
The ClassLoaderContext is a special shared library.

[03-21 10:35:57.820 914:997 I/ActivityManager]
Start proc 14534:com.android.webview:sandboxed_process0/u0i22 for webview_service com.fsck.k9/org.chromium.content.app.SandboxedProcessService0

[03-21 10:35:59.285 914:11057 I/ActivityManager]
START u0 {act=android.intent.action.VIEW dat=content://com.fsck.k9.tempfileprovider/temp/c7066efdfc06ce2bdf4c7aea9f03be3aab55a3f4?mime_type=application/octet-stream typ=application/octet-stream flg=0x10080001 cmp=android/com.android.internal.app.ResolverActivity} from uid 10272

[03-21 10:36:06.446 14216:14224 I/chatty]
uid=10272(com.fsck.k9) FinalizerDaemon identical 1 line

[03-21 10:41:01.212 914:3324 I/ActivityManager]
START u0 {act=android.intent.action.SEND dat=content://com.dp.logcatapp.file_provider/logs/logcat_03-21-2020_10-40-53.txt typ=text/plain flg=0xb080001 cmp=com.fsck.k9/.activity.MessageCompose clip={text/plain U:content://com.dp.logcatapp.file_provider/logs/logcat_03-21-2020_10-40-53.txt} (has extras)} from uid 10104

[03-21 10:41:01.583 914:993 I/ActivityManager]
Displayed com.fsck.k9/.activity.MessageCompose: +331ms

[03-21 10:41:01.708 480:514 W/SurfaceFlinger]
Attempting to set client state on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:41:01.708 480:514 W/SurfaceFlinger]
Attempting to destroy on removed layer: Splash Screen com.fsck.k9#0

[03-21 10:41:01.709 480:1182 W/SurfaceFlinger]
Attempting to set client state on removed layer: Surface(name=1c390bc Splash Screen com.fsck.k9)/@0xf2def08 - animation-leash#0

[03-21 10:41:01.709 480:1182 W/SurfaceFlinger]
Attempting to destroy on removed layer: Surface(name=1c390bc Splash Screen com.fsck.k9)/@0xf2def08 - animation-leash#0

[03-21 10:41:10.470 480:1182 W/SurfaceFlinger]
Attempting to set client state on removed layer: com.fsck.k9/com.fsck.k9.activity.MessageCompose#0

[03-21 10:41:10.470 480:1182 W/SurfaceFlinger]
Attempting to destroy on removed layer: com.fsck.k9/com.fsck.k9.activity.MessageCompose#0

[03-21 10:41:13.984 914:8761 W/NotificationService]
Toast already killed. pkg=com.fsck.k9 callback=android.app.ITransientNotification$Stub$Proxy@2383cd4


```

Changed files:
app/ui/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java

diff --git a/app/ui/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java b/app/ui/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
index a8d2c0165..e4e55485c 100644
--- a/app/ui/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
+++ b/app/ui/src/main/java/com/fsck/k9/ui/messageview/MessageViewFragment.java
@@ -6,6 +6,7 @@ import java.util.Locale;
 
 import android.app.Activity;
 import android.app.DownloadManager;
+import android.content.ActivityNotFoundException;
 import android.content.Context;
 import android.content.Intent;
 import android.content.IntentSender;
@@ -822,7 +823,11 @@ public class MessageViewFragment extends Fragment implements ConfirmationDialogF
         intent.putExtra(Intent.EXTRA_TITLE, attachment.displayName);
         intent.addCategory(Intent.CATEGORY_OPENABLE);
 
-        startActivityForResult(intent, REQUEST_CODE_CREATE_DOCUMENT);
+        try {
+            startActivityForResult(intent, REQUEST_CODE_CREATE_DOCUMENT);
+        } catch (ActivityNotFoundException e) {
+            Toast.makeText(requireContext(), R.string.error_activity_not_found, Toast.LENGTH_LONG).show();
+        }
     }
 
     private AttachmentController getAttachmentController(AttachmentViewInfo attachment) {