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
- 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 );
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
- 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 (). openinputstream (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 ){
- }
- }
- }
- }
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
- </Application>
- <Uses-Permission Android: Name =
"Android. Permission. read_sms"
/>