0

I created an app with action click to send email with image attachment file, I was think the code is working right, after I found the image size of attachment is 0kb, and when I clicked it, it said "Unable find the item", here is the code I use for

public void SendEmailWithAttachment(final String imageUrl){
    String path = "file:///android_asset".concat(File.separator).concat(getString(R.string.sa_books_directory)).concat(File.separator); // Get the path file from my asset folder

    Intent emailIntent = new Intent(Intent.ACTION_SEND);
    emailIntent.setType("image/jpeg");
    emailIntent.putExtra(android.content.Intent.EXTRA_EMAIL, "");
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "This is subject");
    emailIntent.putExtra(android.content.Intent.EXTRA_TEXT, "This is email body");

    emailIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(path + "IndividualVillas/pages/" + imageUrl + ".jpg"));

    startActivity(emailIntent);  
}

I don't know where is the problem, I had tried to change the setType but it doesn't help me also. Any kind help will much appreciate :)

Sorry for my bad English

ilovebali
  • 513
  • 2
  • 7
  • 20
  • Most likely your path is incorrect. What is imageUrl here? Is it web link.Can you post a sample uri which you are creating? – Ajit Pratap Singh Aug 14 '14 at 07:58
  • Hi @AjitPratapSingh imageUrl is just a image name ` downloadPicture.setOnClickListener(new WebView.OnClickListener() { @Override public void onClick(View v) { sendEmailWithAttachment("sample_name_of_image"); } });` thanks for the comment :) – ilovebali Aug 14 '14 at 08:06

2 Answers2

2
    Intent emailIntent = new Intent(android.content.Intent.ACTION_SEND);
    emailIntent.setType("image/jpeg");
    emailIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, subject);
    //emailIntent.putExtra(android.content.Intent.EXTRA_TEXT,body);
    emailIntent.putExtra(Intent.EXTRA_TEXT, Html.fromHtml(new StringBuilder()
    .append("I think you'll like this ")
    .append(wineName).append(".")
    .append("<br /><br />Scanned it with the ")
    .append(bottleRating+tastingNote)

    emailIntent.putExtra(Intent.EXTRA_STREAM,Uri.parse("file://"+winePic));

    final PackageManager pm = ShareWineActivity.this.getPackageManager();
    final List<ResolveInfo> matches = pm.queryIntentActivities(emailIntent, 0);
    ResolveInfo best = null;
    for (final ResolveInfo info : matches)
        if (info.activityInfo.packageName.endsWith(".gm")|| info.activityInfo.name.toLowerCase().contains("gmail"))
            best = info;
    if (best != null)
        emailIntent.setClassName(best.activityInfo.packageName,best.activityInfo.name);
    startActivityForResult(emailIntent, 2015);
Sandeep Tiwari
  • 2,042
  • 3
  • 24
  • 47
  • Hi, thanks a lot for your code, but I think it's almost same with mine (I'm not try it yet :D), I'm looking for where my problem is and not a finish code, or if you think your code is work can you tell me where is my problem??? thanks a lot for your help – ilovebali Aug 14 '14 at 08:11
  • check your image path, I think there is some problem in that. – Sandeep Tiwari Aug 14 '14 at 08:30
  • Hi I read again your code and I see `Uri.parse("file://"+winePic)` with 2 "**//**" I used 3 "///" is that make a problem also???thanks... I will try use 2 and see what happen, thanks – ilovebali Aug 14 '14 at 08:43
  • please mark right of this Answer, By which other can take help. – Sandeep Tiwari Aug 14 '14 at 08:57
  • It still like was, I can see the file attach on email but again the file size is 0kb, I tried to log the path for attaching the file, the path is correct nothing problem with the path, I think I did something wrong but I can not figure it out what it is :( – ilovebali Aug 14 '14 at 09:07
  • Test your method by putting any other image path of sd-card. – Sandeep Tiwari Aug 14 '14 at 09:17
  • Hmmm, I use samsung tab 2 without SD-card, in my method I tried get image from my android asset folder. – ilovebali Aug 14 '14 at 09:37
  • 1
    you follow this steps http://stackoverflow.com/questions/4447477/android-how-to-copy-files-in-assets-to-sdcard Or http://stackoverflow.com/questions/5888718/android-share-images-from-assets-folder or http://stackoverflow.com/questions/17568088/image-preview-in-email-intent-not-showing-when-loaded-from-assets-folder?rq=1 – Sandeep Tiwari Aug 14 '14 at 10:07
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/59398/discussion-between-ilovebali-and-sandeep-tiwari). – ilovebali Aug 15 '14 at 08:46
  • Hi , copying file into SD card and send it to email, that solution is working, but still can not working good if I used file from the asset folder, even I used File Provider. Now the question is what if the user dont have SD card inserted into they are android device :) – ilovebali Aug 18 '14 at 03:23
0

Is the path correct? If you have double checked that then:

The problem here are privileges. If you want to make some private file accessible for other application then probably you want to use this simple, nice and clean solution: https://developer.android.com/reference/android/support/v4/content/FileProvider.html

RobertM
  • 287
  • 1
  • 6
  • Yes, the path is correct, I already double check it, the file is attached on email and the name is show up, but the size is 0KB and when I clicked it said "Unable to find the item", thanks for the suggestion, I will try to learn the link you give me, thanks buddy :) – ilovebali Aug 14 '14 at 08:24