0

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);
}};
NewUser
  • 3,729
  • 10
  • 57
  • 79
user1409172
  • 97
  • 1
  • 11
  • Just make sure about file extension `private static final String FILENAME = "data.pic";` I suggest you to use `private static final String FILENAME = "data.jpeg";` So its either `"data.jpeg"` or `"data.png"`. – user370305 Nov 24 '12 at 16:00
  • look here: http://stackoverflow.com/questions/10527536/android-send-mms-message-with-picture-on-sdcard the difference is the startActivityForResult() is used instead of startActivity() – blacharnia Nov 24 '12 at 16:02
  • Hmm changed it to data.png (as it is a png image) and no luck. – user1409172 Nov 24 '12 at 16:04
  • @blacharnia That was the reference I used for my current attempt. I'm missing something and cant quite put my finger on it. – user1409172 Nov 24 '12 at 16:07
  • 1
    Look at my answer. Hop you can understand it. – user370305 Nov 24 '12 at 16:20

1 Answers1

2

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.

user370305
  • 108,599
  • 23
  • 164
  • 151
  • Also before sending image check whether it exist on external storage or not. – user370305 Nov 24 '12 at 16:18
  • 1
    Greatly appreciate it, I'm a novice and made a rookie mistake lol and see the problem now. Managed to get it properly attaching the file with your help tho now it seems to be having a problem displaying the image in the MMS. Its showing a generic image that has a triangle with a "!" in it, seems its not converting it properly now. (also wanted to get basic functionality working before adding the check if external is available, will be adding it). – user1409172 Nov 24 '12 at 16:30
  • @user1409172 can you send audio or video through mms? i am getting problem sending audio file through mms only for few device like htc,lava or some samsung. – Harshid Jan 29 '13 at 13:22