Send an Image, ACTION_SEND


Last Updated:

  1. JoshKraker

    JoshKraker Member This Topic's Starter

    Joined:
    Dec 3, 2009
    Messages:
    21
    Likes Received:
    0
    Code (Text):
    1.                 MapImage Img = (MapImage) ((Button) v).getTag();
    2.                 ByteArrayOutputStream Os = new ByteArrayOutputStream();
    3.                 Img.Bm.compress(CompressFormat.JPEG, 100, Os);
    4.                 byte[] ImgBytes = null;
    5.                 try {
    6.                     Os.flush();
    7.                     ImgBytes = Os.toByteArray();
    8.                     Os.close();
    9.                 } catch (IOException e) {}
    10.                 Intent i = new Intent(Intent.ACTION_SEND) ;
    11.                 i.setType("image/jpeg");
    12.                 i.putExtra(Intent.EXTRA_STREAM, ImgBytes);
    13.                 MyMv.getContext().startActivity(Intent.createChooser(i,"Send Image To:"));
    The above code opens the Chooser dialog and shows all my option when when I click on any, they either throw an error (gmail), give a message saying "file not available" (picasa), or in SMS's case, opens a new message but no picture is shown.

    Most of the examples I saw for sending an image using the chooser dialog are sending from a file or stored content somewhere (putExtra(EXTRA_STREAM, Uri)). Does anyone know the correct way to send when all we have is a Bitmap?
     

    Advertisement
  2. JoshKraker

    JoshKraker Member This Topic's Starter

    Joined:
    Dec 3, 2009
    Messages:
    21
    Likes Received:
    0
    Ok, so I gave in and tried saving the image to the sdcar first anf then creating a file URI using the following code

    Code (Text):
    1.  
    2.  
    3.     // Omitted Class ...
    4.  
    5.     public Uri getThumbFileUri() {
    6.         if (getThumbBm() == null) return null;
    7.         File F = new File("file:///sdcard/MyProject/Images/Thumbs/" + PicId + ".jpg");
    8.         Uri U = Uri.fromFile(F);
    9.         return U;
    10.     }
    11.  
    Code (Text):
    1.  
    2.  
    3.      // New Chooser Code ...
    4.  
    5.                 MapImage Img = (MapImage) ((Button) v).getTag();
    6.                 Intent i = new Intent(Intent.ACTION_SEND);
    7.                 i.setType("image/jpeg");
    8.                 i.putExtra(Intent.EXTRA_STREAM, Img.getThumbFileUri());
    9.                 MyMv.getContext().startActivity(Intent.createChooser(i,"Send Image To:"));
    10.  
    11.  

    I still get the Chooser dialog with all the same options of my previous post but when I select a send method I now get different errors

    Gmail: Opens a new email showing an attachment of the file I am trying to attach with the same KB size, but when I check the email from them receiver, there is no attachment.

    Messager: Shows an error "You cannot add this picture to your message"

    Picasa: Shows an error "File not available"




    Does that help any?
     
  3. trackaroo

    trackaroo Member

    Joined:
    Dec 12, 2009
    Messages:
    8
    Likes Received:
    0
    Try inserting saving the image to the image gallery using the Media class and used the returned URL like this:

    Code (Text):
    1.  
    2. File imageFile = new File(Constants.SYSTEM_DYNOMASTER_DIR,filename);
    3. FileOutputStream fout = new FileOutputStream(imageFile);
    4. blankBitmap.compress(CompressFormat.JPEG, 100, fout);
    5. fout.flush();
    6. fout.close();
    7.        
    8. String url = Media.insertImage(context.getContentResolver(), imageFile.getAbsolutePath(), imageFile.getName(), imageFile.getName());
    9.  
    10. Intent shareIntent = new Intent(Intent.ACTION_SEND);
    11. shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    12. shareIntent.putExtra(Intent.EXTRA_SUBJECT, subject);
    13. shareIntent.putExtra(Intent.EXTRA_TEXT, body);
    14. shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(url));
    15. shareIntent.setType("image/jpeg");
    16. context.startActivity(shareIntent);
    17.  
    18.  
    Jeff
     
  4. JoshKraker

    JoshKraker Member This Topic's Starter

    Joined:
    Dec 3, 2009
    Messages:
    21
    Likes Received:
    0
    I tried it and it worked so I guess it gets me somewhere. However, I do not want to insert the image into the user's pictures, so is there a way to remove it once done?
     
  5. JoshKraker

    JoshKraker Member This Topic's Starter

    Joined:
    Dec 3, 2009
    Messages:
    21
    Likes Received:
    0
    Ok, I figured it out. Your GRANT_READ.. permission got me thinking. I use the Java.Io to save my cached images to the SdCard and I'm passing the Uri to that file to the Intent. I guess it sandboxed my sdcard files so the intent was unable to read them, even if I gave it the GRANT_READ..

    When I converted to using Context.openFileInput() to write the the file everything worked as expected.

    Still, out of curiosity, it would be nice to know how to save files to the SdCard so that other Intents are able to read them.
     
  6. trackaroo

    trackaroo Member

    Joined:
    Dec 12, 2009
    Messages:
    8
    Likes Received:
    0
    Can you post your code snippet? I'm curious as to what you did. Thanks.

    Jeff
     
  7. JoshKraker

    JoshKraker Member This Topic's Starter

    Joined:
    Dec 3, 2009
    Messages:
    21
    Likes Received:
    0
    Sure,

    I first save a Bitmap to the phone like so:

    Code (Text):
    1.  
    2. FileOutputStream Os = getAppContext().openFileOutput("Img" + _PicId + ".jpg", Context.MODE_WORLD_READABLE);
    3. Bm.compress(Bitmap.CompressFormat.JPEG, 100, Os);
    4. Os.close();
    5.  
    Then I get a Uri to send to the Chooser intent

    Code (Text):
    1.  
    2.                 File F = getAppContext().getFileStreamPath("Img" + _PicId + ".jpg");
    3.                 Uri U = Uri.fromFile(F);
    4.  
    5.                 Intent i = new Intent(Intent.ACTION_SEND);
    6.                 i.setType("image/jpg");
    7.                 i.putExtra(Intent.EXTRA_STREAM, U);
    8.                 getContext().startActivity(Intent.createChooser(i,"Send Image To:"));
    9.  
     
  8. tc_uzair@yahoo.com

    tc_uzair@yahoo.com New Member

    Joined:
    Apr 20, 2010
    Messages:
    1
    Likes Received:
    0
    package com.example.Mms;
    import android.app.Activity;
    import android.content.Intent;
    import android.os.Bundle;
    public class Mms extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent i = new Intent(Intent.ACTION_SEND) ;

    String imageUri = null;
    i.putExtra(Intent.EXTRA_STREAM,imageUri) ;
    i.setType("image/jpeg") ;
    startActivity(Intent.createChooser(i,"Send Image To:")) ;
    }
    }
    I want to write a program which send pictures from one folder,one by one via mms to one fix no..

    I have searched this program from Internet.When em running it through emulator it is opening a text box..I wrote text and added a no..and press the send button..but it is not showing a picture send via MMS
    Please help me out..Thanks :)
     
  9. pedroteixeira07

    pedroteixeira07 New Member

    Joined:
    Oct 19, 2009
    Messages:
    4
    Likes Received:
    0
    Have you found anyway to do this without saving the picture into the phone ? I'd like to send an image file that my app gets from my server and then be able to send it.
     
  10. golemnagesh

    golemnagesh Member

    Joined:
    Dec 28, 2010
    Messages:
    7
    Likes Received:
    0
    Hello Experts,

    now i am working on one android application which needs
    1) to play an audio song to callee when call is lifted.
    2) call has to be disconnected after audio playing completed.

    please help me Experts if you have any suggestions for me ...

    Thank you in advance..please do reply
     

Share This Page

Loading...