Android realizes the addition and deletion function of MMS accessories _android

Source: Internet
Author: User

This article describes the Android implementation of MMS add-on and delete features. Share to everyone for your reference, specific as follows:

Add attachment

In the composemessageactivity.

addattachment (int type) function

Divided into 6 case according to the type difference

Case A:

Mediaselectlistactivity.add_image Use Gallery to select pictures:

Messageutils.selectimage (this, request_code_attach_image);

Up a intent:

Intent innerintent = new Intent (intent.action_get_content);
Innerintent.settype (ContentType); Image Type
Intent wrapperintent = intent.createchooser (innerintent, null);
Startactivityforresult (Wrapperintent,requestcode);

Createchooser function New one Intent Intent = new Intent (action_chooser);

Which is a action_chooser activity.

Case B:

Mediaselectlistactivity.take_picture

Intent Intent = new Intent (mediastore.action_image_capture);
Intent.putextra (Mediastore.extra_output, Mms.ScrapSpace.CONTENT_URI);
Startactivityforresult (Intent, request_code_take_picture);

Took a camera to take a picture,

Case C:

Mediaselectlistactivity.add_video

It's like case a.

Only in type, change from image to video

Case D:

Mediaselectlistactivity.record_video

A VCR like case B, but this time it has a space size calculation: Leave 1024Byte to the text.

According to a MMS 300k calculation, the maximum use of video recording 299k. That's 299*1024byte.

Intent Intent = new Intent (mediastore.action_video_capture);
Startactivityforresult (Intent, request_code_take_video);

Case E:

Mediaselectlistactivity.add_sound

Messageutils.selectaudio (this, request_code_attach_sound);
Intent Intent = new Intent (ringtonemanager.action_ringtone_picker);

Case F:

Mediaselectlistactivity.record_sound

Like B, seven recorders. This type is aution.

Intent Intent = new Intent (intent.action_get_content);
Intent.settype (CONTENTTYPE.AUDIO_AMR);
Intent.setclassname ("Com.android.soundrecorder", "Com.android.soundrecorder.SoundRecorder");

Case G:

Mediaselectlistactivity.add_slideshow

Slide slideshow more embarrassing. Because the entire MMS attachment is also called slideshow, and here the slideshow is more than a piece of pictures together meaning. MMS attachment is also often a number of pictures ... It's going to be a dead start from the name.

Uri Datauri = Mworkingmessage.saveasmms (false);
Intent Intent = new Intent (this, slideshoweditactivity.class);
Intent.setdata (Datauri);
Startactivityforresult (Intent, request_code_create_slideshow);

Saveasmms function Inside there are a few things: first of all, the message to force into MMS, the contents of the message encapsulated into the Pdupersister (can be understood as MMS body), new one sendreq is also MMS head. Generates a URI that contains the MMS header and body.
Finally, an awkward slideshoweditactivity. This thing is too troublesome. No, look.

Then came the Onactivityresult function. The applications that read the media data from the outside activity are back here when they return.

A. Image

The picture in Onactivityreuslt will get the URI, called:

AddImage (URI, false);

This function will call:

Copy Code code as follows:
Mworkingmessage.setattachment (Workingmessage.image, Uri, false);

That is, a append mode to add a picture.

If the picture is too large, asynchronous picture compression functions are enabled.

B Take a picture

Also returns a URI to the ONACTIVITYREUSLT function. It is also called AddImage (URI, False), ditto.

C Select Video file D take video

are called:

Addvideo (Data.getdata (), false);

Data.getdata () is also getting Uri,addvideo calls:

Copy Code code as follows:
Mworkingmessage.setattachment (Workingmessage.video, Uri, append);

The same as the image processing.

E Select audio file F recording

It's all Addaudio-->.
Mworkingmessage.setattachment (Workingmessage.audio, Uri, false);
Not much.

Delete attachment

There is a handler inside the Attachmenteditor, used to send messages to composemessageactivity.

All buttons for removing attachment actions are on attachmenteditor. There are different buttons for different media types, but when pressed, the exits are the same:

Message msg = Message.obtain (Mhandler, msg_remove_attachment);
Msg.sendtotarget ();

That's the operation.

The reason is the same because all the nearby are Slideshowmodel, and this slideshowmodel is:

Arraylist<slidemodel> Mslides; Composed of a list of children slide.

Each slide can contain video,image,audio,text, in which the first three are generally not 22 simultaneous, the only exception being image and audio.

(In fact, I think that if each slide can only contain one of the three, that is, the logic may be clearer without dealing with the above exception)
Say back to the remove operation.

The Handlemessage function in Composemessageactivity's handler, after receiving this delete msg is:

Mworkingmessage.setattachment (Workingmessage.text, NULL, FALSE);

The last false indicates a append mode, that is, the attachment is modified from the new one.

What is Mworkingmessage?

is a text message (including MMS) all the state of the operation of all data of the status quo, mainly several members:

Mmmsstate MMS Status, is not MMS, why is MMS, there are attached to the title, or forced MMS, etc.

Mattachmenttype attachment type. If Mslideshow is multiple pages: slide type.   Single page: Pictures | sound | video | text. If the mslideshow is empty, it is the text type.

Mslideshow attachment data Array. That's the arraylist<slidemodel> mslides.

Now look back at the delete operation.

The main function in Setattachment is Changemedia (type, Datauri), where the parameter type passed is Text,datauri null.
This function goes in:

Slidemodel slide = mslideshow.get (0);
Mediamodel Media;
Remove any previous attachments.
Slide.removeimage ();
Slide.removevideo ();
Slide.removeaudio ();
If we ' re changing to-text, just bail out.
if (type = = TEXT) {return
  ;
}
Make a correct mediamodel for the type of attachment.
if (type = = IMAGE) {
  media = new Imagemodel (Mcontext, Uri, Mslideshow.getlayout ()
      . Getimageregion ());
else if (type = = Video) {
  media = new Videomodel (Mcontext, Uri, Mslideshow.getlayout ()
      . Getimageregion ()); 
   } else if (type = = AUDIO) {
  media = new Audiomodel (Mcontext, URI);
} else {
  throw new Illegalargumentexce Ption ("Changemedia type=" + type
      + ", uri=" + URI);
}
Add it to the slide.
Slide.add (media);
For video and Audio, set the duration of the slide to
/the attachment.
if (type = = Video | | | type = = AUDIO) {
  slide.updateduration (media.getduration ());
}

See the first return and we can return.

How neat! Direct MMS Original attachment look all unbearable directly a knife deleted, type regression to Workingmessage.text, put the URI empty.

Also, say some of the questions outside.

This Changemedia function, to and fro, is all modified Mslideshow.get (0) that

In Setattachment, if the Append mode is used, then the Appendmedia is used instead of the Changemedia function.

For Append mode,

If the last page contains image image or video vedio, then append must be added to the next one.

The sense of the source code in this judgment is a bit complicated. You see me a word can be summed up, he wrote the code half a day ~

But I can't write a better ~ ~

There is also the add function of Slidemodel. A lot of things are stacked together, so it's a bit complicated.

The key function added is the following, the first parameter is the original media of the corresponding format (for example, you want to add video, that is the original video, can be null), the second is the new media added

 private void Internaladdorreplace (Mediamodel old, Mediamodel media) {int addsize = Me
  Dia.getmediasize ();
  int removesize;
    if (old = = null) {if (null!= mparent) {mparent.checkmessagesize (addsize);
    Mmedia.add (media);
    Increaseslidesize (addsize);
  Increasemessagesize (addsize);
    else {removesize = Old.getmediasize ();
      if (Addsize > Removesize) {if (null!= mparent) {mparent.checkmessagesize (addsize-removesize);
      } increaseslidesize (Addsize-removesize);
    Increasemessagesize (addsize-removesize);
      else {decreaseslidesize (removesize-addsize);
    Decreasemessagesize (removesize-addsize);
    Mmedia.set (old), media (Mmedia.indexof);
  Old.unregisterallmodelchangedobservers ();
  for (Imodelchangedobserver observer:mmodelchangedobservers) {MEDIA.REGISTERMODELCHANGEDOBSERVER (Observer); }
}

The

also has an asynchronous narrowing function when the attachment is too large, which is the following function

public static void Resizeimageasync (final context, final Uri Imageuri, final Handler Handler, final Res Izeimageresultcallback CB, Final Boolean append) {//show a progress toast if the resize hasn ' t finished//within
   One second.
   Stash the runnable for showing it away so we can cancel//It later if the resize completes the ahead. Final Runnable showprogress = new Runnable () {public void run () {Toast.maketext (context, R.string.compres
     Sing, Toast.length_short). Show ();
   }
   };
   Schedule it for one second from now.
   Handler.postdelayed (showprogress, 1000);
       New Thread (New Runnable () {public void run () {final Pdupart part;
         try {uriimage image = new Uriimage (context, Imageuri);
             Part = Image.getresizedimageaspart (mmsconfig. Getmaximagewidth (), Mmsconfig.getmaximageheight (),
       Mmsconfig.getmaxmessagesize ()-message_overhead); } finallY {//Cancel pending show of the progress toast if necessary.
       Handler.removecallbacks (showprogress);
         } handler.post (New Runnable () {public void run () {Cb.onresizeresult (part, append);
     }
       });
}). Start ();

 }

The picture is scaled to the maximum 640*480, if still greater than the 300*1024-5000 byte (almost 295k), then zoom to 295k.

This size is written by the source code programmer in the sense of death.

The cb.onresizeresult here is the function in the composemessageactivity resizeimageresultcallback of the call.

After the processing size is finished, the new picture will be taken to setattachment again, and the attachment is updated.

For more information on Android-related content readers can view the site topics: "Android Database Operating skills summary", "Android programming activity Operation Skills Summary", "Android File Operation skills Summary", " Android programming development of the SD card operation method Summary, "Android Development introduction and Advanced Course", "Android Resource Operation skills Summary", "Android View tips Summary" and "Android Control usage Summary"

I hope this article will help you with the Android program.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.