0

Possible Duplicate:
Android multiple email attachments using Intent

I already know there are may similar questions as I got my current code from one of the answer. But I am facing some problem and initially I think it is of gmail app but then I tried with other email client and same issue. Code:--

ArrayList<Uri> uris = null;
        try {
            uris = logger.getSiteFilePaths(siteIndex);
        } catch (BaseLoggerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        Intent emailIntent = new   Intent(android.content.Intent.ACTION_SEND_MULTIPLE);
        emailIntent.setType("application/octet-stream");
        emailIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
        emailIntent.putExtra(Intent.EXTRA_EMAIL,"");
        emailIntent.putExtra(Intent.EXTRA_SUBJECT, "Bl\u00fc-Test Log");
        emailIntent.putExtra(Intent.EXTRA_TEXT, "Please find the attachments.");
        emailIntent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        startActivity(Intent.createChooser(emailIntent, "Email:"));

File paths are accurate

**e.g. --** file:///storage/sdcard0/BAPISandy_test1.csv

Issue:- In Gmail app files seems to be attached but when you send email it says "Couldn't show attachemnt" in notification bar. If tried with other email client it doesn't show files as attachment. System Info:-- Samsung Galaxy Tab 2 (7 inch) Android 4.1.1

Community
  • 1
  • 1
Sandip Jadhav
  • 7,377
  • 8
  • 44
  • 76

2 Answers2

1

Sorry All, Its hard to find small bugs like missing "/" in file path which actually causing issue.

Sandip Jadhav
  • 7,377
  • 8
  • 44
  • 76
1

I've fallen foul of this today too on my Note 2 and with a customer with a SG3. I did a bit of digging and discovered the following.

If I request the external storage device name - Environment.getExternalStorageDirectory().getName() (I use this as I'm targetting ver 7) - I get 'sdcard0', if I then add my filename and pass that to email or gmail then neither one will add the attachment. If I manually edit the zero off the end of 'sdcard0' and pass 'sdcard' + my filename then it all works just fine. None of this seem to be a problem before JB or if I run this code under JB in the emulator which always returns 'sdcard'.

I'm not sure if there is different code that I am supposed to use or if this is an inconsistency in the way the os reports the path and the email systems use it. (I had thought that sdcard0 was supposed to point to the same place as sdcard - but something isn't working)

I may just patch my code to strip off the zero (which I know is ugly & dangerous). Unless anyone else has any bright ideas :)

Edit: I have added my code below in case anyone else might find it useful.

                    // try and grab the log file

            String logFile = File.separator + getResources().getString(R.string.app_name) + File.separator + "logs" + File.separator + Constants.SMS_FILE;
            String extStorage = Environment.getExternalStorageDirectory().getName();

            // The following is a kludge for the Samsung (and maybe more) JB devices that don't allow you to add a file with the SDCARD0 external storage
            // link

            File nf = new File(extStorage, logFile);
            if (!nf.exists()) {
                // OK we can't find the file - say so and see if we are hiding behind SDCARD0

                Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "File: " + extStorage + logFile + " Doesn't exist - trying again");
                if (extStorage.matches("(?i)SDCARD(?-i)[0-9]")) {
                    // OK we've got a SDCARDx scenario - let's see if the file exists without the x
                    String extStorageFix = extStorage.replaceFirst("(?i)SDCARD(?-i)[0-9]", extStorage.substring(0, 6)); // try it without the 0,1,2 etc
                    nf = new File(extStorageFix, logFile);
                    if (!nf.exists()) {
                        // OK we're in the pooh now - can't find the log file so just say so and try to exit without attaching it.
                        Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "File: " + extStorageFix + logFile + " Doesn't exist either - giving up");
                        Toast.makeText(
                                getApplicationContext(),
                                "Unable to attach Log file from\n" + extStorage + logFile + "\n" + extStorageFix + logFile
                                        + "\nPlease add it manually if possible", Toast.LENGTH_LONG).show();
                    } else {
                        // Hurrah we found it - remember we did so and carry on
                        extStorage = extStorageFix;
                        Log.w(getLocalClassName() + ".shouldOverrideUrlLoading", "Found logfile at: " + extStorage + logFile);
                    }
                }

            }

            if (Debug.ON) {
                Log.d(getLocalClassName() + ".shouldOverrideUrlLoading", "Filepath = " + "file://" + File.separator + extStorage + logFile);
            }
            i.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + File.separator + extStorage + logFile));**strong text**
Richard
  • 135
  • 1
  • 7
  • Just stepped on this today - during a demo of "working" code :)) I couldn't find any other solutions than the hack you mention, Richard. Have you ? thanks - H – Howard Pautz Mar 28 '13 at 20:52
  • Howard - Unfortunately not :( I have had to patch my live code to do the following: Try with the normal returned path. If this fails see if there is a digit on the end and if so strip it off and try to find the file again. This seems to have worked so far. :) I'm not sure if this is just a Samsung JB problem or if applies to other makes as well. – Richard Mar 31 '13 at 18:13
  • Thanks, Richard ... this was on the slow burner for me, but have to deal with it now. Interesting, so it's not the lack of a trailing slash, but rather the digit on the end that causes the problem ??? I.e., I might get back something like "/storage/sdcard1" (and is "storage/sdcard9" possible ?) when the real path (sans file name) should be "/storage/sdcard/" ?? – Howard Pautz Apr 22 '13 at 00:36
  • Howard - My issue was definitely the number. As far as I can tell the data all resides in one place and SDCARD, SDCARD0 etc. are just pointers to it. This has only been an issue with Samsung & JB as far as I can tell (and only when adding a file to email - normal access works fine) The emulator seems to work fine in all circumstances too. I believe that other numbers are also possible The following link may be useful http://forum.xda-developers.com/showthread.php?t=1795140 I've not had any further customer issues since I patched the code although it isn't an elegant solution. – Richard Apr 24 '13 at 16:51
  • Richard - thanks "... normal access works fine " - oops, it fails trying to save ANY file: m_sFilePath = Environment.getExternalStorageDirectory().getAbsolutePath() ; give's /mnt/sdcard in os < JellyBean, but /storage/sdcard0 in JB, as XDA posts say, are symlinks to /mnt/sdcard. But the bizarre part: append a file name with a leading slash and the slash is there in os < JB, but gets trimmed out (somehow?) in JB ! e.g. /mnt/sdcard/MyFile.bmp VS /storage/sdcard0MyFile.bmp <--- using EXACTLY the same code !! (is it actually a C, null terminated string ? Nope, the '0' is counted in length() call. – Howard Pautz Apr 24 '13 at 21:05
  • See Samsungs site http://developer.samsung.com/forum/board/thread/view.do?boardName=GeneralB&messageId=162934&messageNumber=1381&startId=zzzzz~&searchType=TITLE&searchText=sdcard – Howard Pautz May 01 '13 at 17:59
  • Howard - thanks for the link, it's an interesting read. I think I'll stick with my method as that seems to work regardless of the device (for me at least) – Richard May 02 '13 at 23:38
  • Richard - what I don't get is why your exact code *doesn't* work on several Samsung devices (which have an sdcard0) ... trimming out the digit still causes file not found. – Howard Pautz May 03 '13 at 01:06
  • Just a follow up for anyone else who might stumble here like I did :) the problems I'm having on Samsung devices are with image files, but a few tests also had issues with other types. This is a must read if you are having excruciating pain w/Samsung, et.al. ... and a superb example of the Power of SO ! http://stackoverflow.com/questions/16019165/summary-take-a-picture-utilizing-camera-intent-and-display-the-photo-with-corre – Howard Pautz May 03 '13 at 21:00