Search code examples
androidsavemms

"Sorry you cannot add this picture to your message" when attempting to send an MMS


Im trying to send an MMS with a picture that I have saved to the SDcard and while opening the messaging service I get the statement "Sorry you cannot add this picture to your message" and it doesn't attach the file. I've tried parsing the file into a uri, tried passing the file directly into the MMS intent, and a few other things. I'm not sure what I'm missing and I'm pretty certain it is saving it as I can see the file in the File Viewer. Do I have to make the image available to the mediastore by scanning it (prefer not to put it in the image gallery), do I have to open the file first before passing it in? A little direction as to what I should be doing would be appreciated.

My file

private static final String FILENAME = "data.pic";
File dataFile = new File(Environment.getExternalStorageDirectory(), FILENAME);

Saving the image

// Selected image id
int position = data.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ChosenImageView.setImageResource(imageAdapter.mThumbIds[position]);
Resources res = getResources();
Drawable drawable = res.getDrawable(imageAdapter.mThumbIds[position]);
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
try {
    File file = new File(dataFile, FILENAME);
    file.mkdirs();
    FileOutputStream fos = new FileOutputStream(file);
    fos.write(bitmapdata);
    fos.close();
 }catch (FileNotFoundException e) {
 e.printStackTrace();
 } catch (IOException e) {
 e.printStackTrace();
 }

}

Where I want to attach it and my current attempt

// Create a new MMS intent
Intent mmsIntent = new Intent(Intent.ACTION_SEND);
mmsIntent.putExtra("sms_body", "I sent a pic to you!");
mmsIntent.putExtra("address", txt1);
mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(FILENAME)));
mmsIntent.setType("image/png");
startActivity(mmsIntent);
}};

Solution

  • Aaah Sorry, You can't.

    Problem:

    The new File(FILENAME) is doesn't exist.

    Look at these three different Files code lines..

    1. File dataFile = new File(Environment.getExternalStorageDirectory(), FILENAME);
    
    2. File file = new File(dataFile, FILENAME);
    
    3. Uri.fromFile(new File(FILENAME))
    

    All are have difference reference.

    Solution:

    Change your code

    try {
         File file = new File(dataFile, FILENAME); 
         file.mkdirs(); // You are making a directory here
         FileOutputStream fos = new FileOutputStream(file); // set Outputstream for directory which is wrong
         fos.write(bitmapdata);
         fos.close();
        }catch (FileNotFoundException e) {
         e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
    

    With

    try {
          FileOutputStream fos = new FileOutputStream(dataFile);
          fos.write(bitmapdata);
          fos.close();
         }catch (FileNotFoundException e) {
          e.printStackTrace();
         } catch (IOException e) {
          e.printStackTrace();
        }
    

    And the Main code lines for send MMS,

    if(dataFile.exists())
    {
     Intent mmsIntent = new Intent(Intent.ACTION_SEND);
         mmsIntent.putExtra("sms_body", "I sent a pic to you!");
         mmsIntent.putExtra("address", txt1);
         mmsIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(dataFile));
         mmsIntent.setType("image/png");
         startActivity(mmsIntent);
    }
    

    Just use only one dataFile File reference for all your Code.