4

I'm really new to Java and have been building an app on Sketchware. If you're not familiar with it, it uses block programming and you can inject your own code in custom blocks.

As the storage of all app views are local only, I need all the output PDFs to be attached to an email on the press of a button.

The below code works to attach one file but need 6 files attached. All being called from /Documents/ folder on the android device. How can I achieve this?

emailIntent.putExtra(
    Intent.EXTRA_STREAM,
    Uri.fromFile(
        new java.io.File(Environment.getExternalStorageDirectory() +"/Documents/filename.pdf")
    )
);

The filenames I have are in one folder, and are named filename1.pdf, filename2.pdf, etc.

If I try repeating this code with each filename, filename6.pdf will be the only file attached to the email.

Here's the Sketchware block diagram:

enter image description here

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
Damien
  • 53
  • 5
  • I will always know the file names. The app generates specific files that overwrites previous files. – Damien May 28 '20 at 18:57
  • I gave that a go previously but the last file on the list was the only file attached. – Damien May 28 '20 at 18:59
  • Just to clarify, is the previous stage creating six PDF files of different names? – halfer May 28 '20 at 19:09
  • Yes, the app is 6 pages. Moving to the next page saves a pdf that is stored in Documents. For example, page1.pdf, page2.pdf etc. When the app is used again, these files are overwritten. – Damien May 28 '20 at 19:22
  • Can you supply for readers a link to the docs for `emailIntent.putExtra`? What class is `emailIntent` instantiated from? I wonder if it would take an array as a second parameter. – halfer May 28 '20 at 19:28
  • (I assume that `putExtra()` does not do a send, and just does an attachment operation, with the send done in a subsequent command). – halfer May 28 '20 at 19:29
  • Here's the link for what Sketchware generates in the.java file. https://pastebin.com/jzkC1abg i have changed names of files and taken out identifiable information on websites and app names etc. – Damien May 28 '20 at 20:23
  • The source code does not import to Android Studio and most of the elements are by block code. Google Sketchware to get an idea of what I mean, so alot of the code can't be efficiently edited outside of Sketchwares building platform...not at my skill level anyway. – Damien May 28 '20 at 20:26
  • Hmm, I may be exhausting the limits of my knowledge here - I am not a Java or Android dev. It looks like `mail` is a `android.content.Intent`, which does not sound like it is explicitly for mail sending - it seems to be just a generic data transfer object. Thus I wonder if there is something in Sketchware that reads this and understand that the sending of an email is required. – halfer May 28 '20 at 22:09
  • [This documentation](https://docs.sketchware.io/docs/component-intent.html) does not shed any light on whether more than one file can be added here. – halfer May 28 '20 at 22:10
  • mail is an Intent that gets given an ACTION_VIEW with a setAction command block. mail is just the name I applied myself. Incredibly frustrating, as you can imagine. Do you happen to know if there are any other Java forums I could try? – Damien May 31 '20 at 15:45
  • 1
    I can add a bounty if you like, to attract attention, if you can check back every day for the next week (or until an answer is obtained). New people looking at it might have some questions for you, and bounties can be wasted if the question author is not available to help the helpers. – halfer May 31 '20 at 15:53
  • 2
    That would be an enormous help, thank you. This problem has plagued me for about 6 weeks now. I will be religously checking here while still trying different things to resolve it. So will update if I come up with something in the meantime. – Damien May 31 '20 at 17:05
  • 1
    Great. I can't promise any results - bounties are for eyeballs not results, and it's still just volunteers here, picking and choosing what they fancy looking at. Although we recommend making questions as self-contained as possible, it may not be possible in this case. Can you pop a simple repo on GitHub/Bitbucket etc, so that an intrigued reader can try it without much fuss? – halfer May 31 '20 at 17:18
  • 3
    Your help is much appreciated. Github link for the code linked in the pastebin https://github.com/Swnctt/Java-sketchware.git – Damien May 31 '20 at 22:10

2 Answers2

3

First of all, as other answer suggests, currently Intent.ACTION_SEND_MULTIPLE is the way to send multiple files.

But, not having a feature in built-in blocks of Sketchware is not really the exact limitation of the app, as it provides following block which is able to do anything you want in the android way.

enter image description here

And you have already used this element for adding some custom code. So, for solving your problem, the block will be like this:

enter image description here

And here is the details of some of the custom code blocks I've added:

mail.setAction(Intent.ACTION_SEND_MULTIPLE): This custom code has been added by removing default Intent > setAction block. And the action name says it all, this allows sending multiple files through intent.

ArrayList<Uri> uris = new ArrayList<Uri>(): This declares a new ArrayList to store the list of all Uri to be sent through intent.

uris.add(Uri.fromFile(new java.io.File(Environment.getExternalStorageDirectory() + "/Documents/filename1.pdf"))): This line adds the provided uri to the ArrayList named uris. Call this block as many times as you want to add multiple number of files uri to the list.

mail.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris): This binds the uris to the EXTRA_STREAM of the intent.

Edit:

Starting from Android 7.0 and up, there is some policy change for security purpose. That's why this extra code is added. The block image above is updated with this code already:

StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder();
StrictMode.setVmPolicy(builder.build());

Though it is advised to use android.support.v4.content.FileProvider to solve this type of issue, but for less support in Sketchware platform, at this point it is better to use above method.

You can read this for more clarification of above fix.

UkFLSUI
  • 5,509
  • 6
  • 32
  • 47
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/215484/discussion-between-rakibul-islam-and-damien). – UkFLSUI Jun 07 '20 at 21:33
  • @Damien: if you wish to use code in comments here, you can use `backtick formatting` - it is a symbol often near to the Esc key. It's an inline rather than block monospace style. – halfer Jun 07 '20 at 23:55
1

Maybe that will do the job for you.

Here is the code you need to create an emailIntent that contains multiple attachments. The key change is ACTION_SEND_MULTIPLE.

public static void email(Context context, String emailTo, String emailCC,
    String subject, String emailText, List<String> filePaths)
{
    //need to "send multiple" to get more than one attachment
    final Intent emailIntent = new Intent(Intent.ACTION_SEND_MULTIPLE);
    emailIntent.setType("text/plain");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, 
        new String[]{emailTo});
    emailIntent.putExtra(android.content.Intent.EXTRA_CC, 
        new String[]{emailCC});
    emailIntent.putExtra(Intent.EXTRA_SUBJECT, subject); 
    emailIntent.putExtra(Intent.EXTRA_TEXT, emailText);
    //has to be an ArrayList
    ArrayList<Uri> uris = new ArrayList<Uri>();
    //convert from paths to Android friendly Parcelable Uri's
    for (String file : filePaths)
    {
        File fileIn = new File(file);
        Uri u = Uri.fromFile(fileIn);
        uris.add(u);
    }
    emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
    context.startActivity(Intent.createChooser(emailIntent, "Send mail..."));
}

Update

Post discussion in chat-room I would like to conclude that it is not possible to send multiple attachments in email with Sketchware, as it does not offer Intent.ACTION_SEND_MULTIPLE functionality. You need to send multiple emails with attachments one by one.

Above mentioned code will suffice the job you need when you have the liberty to code and this will work with Android as mentioned here.

All I could read about Sketchware is that only one file can be attached at a time, see here.

halfer
  • 19,824
  • 17
  • 99
  • 186
Kunal Vohra
  • 2,703
  • 2
  • 15
  • 33
  • Hi Kunal, thanks for the suggestion. I have tried to implement it using an onClick function on a button press as it is intended. The Sketchware app has thrown up these errors when exporting for install on my device - https://pastebin.com/xPyZcxTG - I'm not 100% on Java but I don't think Sketchware implements it like you would expect. It's quite unusual. – Damien Jun 02 '20 at 21:07
  • Hi, by seeing error I assume it is because you copied code and white spaces came. I will suggest you to copy only needed code or write it as it is just extended code if you’re which was earlier working. I am not much aware about sketcheare, wrote solution for you based on code I saw in question. If you still have issues, let me know. I will solve – Kunal Vohra Jun 03 '20 at 02:34
  • @Damien: it might be useful for readers if you could add your latest attempt to your repo? – halfer Jun 03 '20 at 12:01
  • Added to repo. As previously described, directly inserting a block of code into Sketchware is done using an "add source directly" block. Viewing the code in the repo is not a good comparison to how the app would be programmed. Quick google of Sketchware will make it clearer. – Damien Jun 03 '20 at 12:55
  • https://chat.stackoverflow.com/rooms/215367/discussion-between-damien-and-kunal Here you go – Kunal Vohra Jun 05 '20 at 12:56
  • Updated my answer post discussion in Chatroom. Sketchware doesn't have the support to send multiple emails. – Kunal Vohra Jun 06 '20 at 08:03
  • Great work, Kunal and @Damien. Is there a bug tracker for Sketchware, where a feature suggestion could be added? – halfer Jun 06 '20 at 18:24
  • Great Idea Halfer. @ Damien please post your issue here and someone will definitely shed light on that. https://github.com/sketchware/sketchware.github.io – Kunal Vohra Jun 07 '20 at 02:21
  • I've just explored that link Kunal, there does not seem to be a bug tracker in use in either of their two repos. @Damien, have you tried chatting to them [via their public Slack channel](http://slack.sketchware.io/)? They have a [documentation domain](https://docs.sketchware.io/) but there is no sign of a bug tracker. – halfer Jun 07 '20 at 10:03
  • Their blog also indicates they've had cashflow problems - I sympathise greatly, but it may nevertheless be a risky proposition to build too much on their platform. – halfer Jun 07 '20 at 10:11
  • Yes. It is for beginners only and not to be used commercially. This is like for school kids to make musical games – Kunal Vohra Jun 07 '20 at 10:45
  • Also, @halfer we can raise issue on their git repo. We just need to establish communication. Thank you, this is my conclusive comment. Glad sharing knowledge with both of you. – Kunal Vohra Jun 07 '20 at 10:51
  • I do not recommend raising issues on either repo. Obviously, neither of them are in use, but moreover, neither is their main product. – halfer Jun 07 '20 at 10:54