0

I have an activity that launches the email intent, passing an array list of Uri objects, that point to local files. This works when there are a small number of files e.g. 3, or 10. However when i have 1000+ files (totalling a size of 14 mb), the activity hangs (which is expected, lots of i/o), however it sometimes hangs indefinitely or when it does return the intent doesn't launch.

Below code (called by an AsyncTask) creates the array list of Uri's and launches Email intent:

private Intent createEmailAndSend(List<FilePath> paths) {
    Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("message/rfc822");
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Email subject");

    ArrayList<Uri> fileUriList = new ArrayList<>();

    for (FilePath filePath : paths) {
        File file = new File(filePath.getPath());
        fileUriList.add(Uri.fromFile(file));
    }

    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUriList);

    startActivity(intent);
}

Testing on:

  • Genymotion, Android Studio 2 Beta Emulator, Nexus 7 and Nexus 6p
  • Android: Minimum 19 (Kitkat) and target is Kitkat

Does anyone know what i'm doing wrong?

SteedsOfWar
  • 543
  • 2
  • 9
  • 23

3 Answers3

1

1000+ files as attachment to a single mail? It has to hang. I see that the total size is not too big but still the number of attachments is huge.

Two solutions I can think of:

  1. Compress and attach a zip as single attachment? (Refer this post for how-to)
  2. Do this in an AsyncTask

With any of the above two in place your Activity wont hang.

Community
  • 1
  • 1
Viral Patel
  • 32,418
  • 18
  • 82
  • 110
  • Thanks, as mentioned above i am already using an AsyncTask, that executes the code off of the main thread. The issue arises when calling startActivity on the email intent. – SteedsOfWar Jan 04 '16 at 19:18
  • then i think you should try and look at compression to a zip file and making it a single attachment. – Viral Patel Jan 04 '16 at 19:25
  • It's not so much the hanging off the app, understandable as you've mentioned. It's around the app crashing on starting the intent (mail client), which is curious. I receive no exceptions or errors, the app crashes and relaunches, no hint of what went wrong. – SteedsOfWar Jan 14 '16 at 13:42
  • Probably the mail client has handled the exception and initiated the restart? – Viral Patel Jan 14 '16 at 13:46
  • 1
    @AndroidMechnic, possibly but difficult to tell for certain. I've ditched attaching so many files. From a users perspective, it makes more sense to handle 1 file over hundreds. – SteedsOfWar Jan 14 '16 at 13:56
  • Great... Makes sense – Viral Patel Jan 14 '16 at 14:29
1

you obviously need to use AsyncTask. please note that any web transaction(download or upload) must not be carried out on the UI thread, and must be dealt with asynchronously.

EDIT 1 : after I was told AsyncTask was used.

the AsyncTask guide reads :

AsyncTasks should ideally be used for short operations (a few seconds at the most.) If you need to keep threads running for long periods of time, it is highly recommended you use the various APIs provided by the java.util.concurrent package such as Executor, ThreadPoolExecutor and FutureTask.

apparently your code runs longer than a few seconds so should probably use one the above. I used this tutorial to learn FutureTask. try this and keep us posted.

milad zahedi
  • 787
  • 6
  • 21
  • Thanks, i should have have mentioned that i already have asyncTask that calls the code that i have shared. The issue arises when launching the email intent. – SteedsOfWar Jan 04 '16 at 19:18
  • @WolfBane I updated the answer, see if it solves the problem and keep me posted. – milad zahedi Jan 06 '16 at 10:09
  • Thanks. I've ended up using AsyncTask and a progressbar. Which works fine for my requirements. Unfortunately the large number of attachments causes the app to crash on launching mail client via an Intent. Therefore i've had to zip the files up into one zip and attach that. Which works fine. – SteedsOfWar Jan 14 '16 at 13:41
0

I thought this maybe useful to others. The solution i found was to zip the files into one zip file and then attach that to the email. Otherwise it will crash with no errors being reported.

HTH

SteedsOfWar
  • 543
  • 2
  • 9
  • 23