Reading MMS files in Android

Source: Internet
Author: User

Reading MMS files in Android apps involves a database/data of Android
/COM. Android. providers. telephony/databases/mmssms. dB and a folder/data/Data
/COM. Android. providers. telephony/app_parts. To back up MMS, you can copy the database and folder to the hard disk, but in the Application
But you cannot use the opendatabase series to read this database or open the file directly to read the attachment file. Because your application is not their owner, you cannot directly read it.
Use contentprovider to read data.

1. Read the mmssms. DB database


MMS headers, sender numbers, dates, and other data are stored in the PDU table of mmssms. DB. to read this table, you can use a contentprovider, Uri provided by the system.
Is "content: // MMS ". The contentprovider is the same as the database. The structure of the contentprovider is also the same as that of the PDU table.
The structure is the same. The Android documentation does not describe these fields, but you can back up the mmssms. DB database to the hard disk and then use the SQLite Database
Browser software to view the analysis, specific can refer to: http://www.blogjava.net/easywu/archive/2010/01/10
/308959.html.
Content: // The main fields of MMS are as follows:
◆ _ ID: The primary key of the MMS Message. It corresponds to the mid field in the part table (the URI of contentprovider is content: // MMS/part. For details, refer to the mid field.
◆ Sub: the title of the MMS.
◆ Date: The Receiving date of the MMS Message.
The following code queries the cotnetprovider, obtains a cursor, and lists all column names.

Java code
  1. Cursor cur = getcontentresolver (). Query (URI. parse (
    "Content: // MMS"

    ),
    Null


    ,
    Null


    ,
    Null


    ,
    Null


    );
  2. String [] temp = cur. getcolumnnames ();
  3. For


    (
    Int


    I =
    0

    ; I <temp. length; I ++)
  4. System. Out. println (I +
    ":"

    + Temp );
        Cursor cur = getContentResolver().query(Uri.parse("content://mms"),null, null, null, null);        String [] temp=cur.getColumnNames();        for (int i=0;i<temp.length;i++)        System.out.println(i+":"+temp); 

After obtaining the cursor through the above method, you can operate on the cursor, get the _ id through the get method, and then read the MMS attachment file according to the _ id.

2. Read the MMS Attachment File


The address of the MMS attachment file is stored in the _ data field of the part table in mmssms. DB, for example, "/data/Data
/COM. Android. providers. telephony/app_parts/part_1262694257763 ", but read MMs in the Application
This field is useless during attachment, because the file cannot be directly read. To read data, you must use contentprovider. The URI is "content: // MMS ".
/Part. The URI corresponds to the part table. You can use the following code snippet to read files:

Java code
  1. String selection =
    New


    String (
    "Mid = '"

    + Key +
    "'"

    );
    // This key is the _ ID in the PDU.


  2. Cursor cur = getcontentresolver (). Query (URI. parse (
    "Content: // MMS/part"

    ),
    Null


    , Selection,
    Null


    ,
    Null


    );

  3. If


    (Cur. movetofirst ())

  4. Do


    {

  5. Int


    _ Partid = cur. getint (cur. getcolumnindex (
    "_ Id"

    ));
  6. String partid = string. valueof (_ partid );
  7. Uri parturi = URI. parse (
    "Content: // MMS/part /"

    + Partid );
  8. Bytearrayoutputstream baos =
    New


    Bytearrayoutputstream ();
  9. Inputstream is =
    Null


    ;

  10. Try


    {
  11. Is = getcontentresolver (). openinputstream (parturi );

  12. Byte


    [] Buffer =
    New


     
    Byte


    [
    256

    ];

  13. Int


    Len = is. Read (buffer );

  14. While


    (LEN> =
    0

    )
  15. {
  16. Baos. Write (buffer,
    0

    , Len );
  17. Len = is. Read (buffer );
  18. }
  19. }
    Catch


    (Ioexception e ){
  20. }
    Finally


    {

  21. If


    (Is! =
    Null


    ){

  22. Try


    {
  23. Is. Close ();
  24. }
    Catch


    (Ioexception e ){
  25. }
  26. }
  27. }
  28. }
String selection = new string ("mid = '" + key + "'"); // This key is the _ ID in the PDU. Cursor cur = getcontentresolver (). query (URI. parse ("content: // MMS/part"), null, selection, null, null); If (cur. movetofirst () do {int _ partid = cur. getint (cur. getcolumnindex ("_ id"); string partid = string. valueof (_ partid); Uri parturi = Uri. parse ("content: // MMS/part/" + partid); bytearrayoutputstream baos = new bytearrayoutputstream (); inputstream is = NULL; try {is = getcontentresolver (). openin Putstream (parturi); byte [] buffer = new byte [256]; int Len = is. read (buffer); While (LEN> = 0) {baos. write (buffer, 0, Len); Len = is. read (buffer) ;}} catch (ioexception e) {} finally {If (is! = NULL) {try {is. Close () ;}catch (ioexception e ){}}}}

The baos obtained here is the attachment file.

3. Declare permission

To use content: // MMS, content: // MMS/part, content: // SMS, and so on in the code, you must also register premission in androidmanifest. xml. The Code is as follows:

Java code
  1. </Application>
  2. <Uses-Permission Android: Name =
    "Android. Permission. read_sms"

    />
Related Article

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.