My app is trying to fetch SMS and MMS from the device and store it in a database. I have tried this code: How to Read MMS Data in Android? This is working fine, but the problem is getting the wrong MMS image. This happens in the scenario when I send a new MMS , while backing up the MMS. Here is my code:
// To get text content from mms..
public ArrayList<String> getMmsTextContent(String mmsId) {
String body = null;
ArrayList<String> arlMMS = new ArrayList<String>();
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cursor = getContentResolver().query(uri, null, selectionPart,
null, null);
if (cursor.moveToFirst()) {
do {
String partId = cursor.getString(cursor.getColumnIndex("_id"));
String type = cursor.getString(cursor.getColumnIndex("ct"));
if ("text/plain".equals(type)) {
String data = cursor.getString(cursor
.getColumnIndex("_data"));
if (data != null) {
// implementation of this method below
body = getMmsText(partId);
arlMMS.add(body);
} else {
body = cursor.getString(cursor.getColumnIndex("text"));
arlMMS.add(body);
}
}
} while (cursor.moveToNext());
return arlMMS;
}
return null;
}
// To get the text
private String getMmsText(String id) {
Uri partURI = Uri.parse("content://mms/part/" + id);
InputStream is = null;
StringBuilder sb = new StringBuilder();
try {
is = getContentResolver().openInputStream(partURI);
if (is != null) {
InputStreamReader isr = new InputStreamReader(is, "UTF-8");
BufferedReader reader = new BufferedReader(isr);
String temp = reader.readLine();
while (temp != null) {
sb.append(temp);
temp = reader.readLine();
}
}
} catch (IOException e) {
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return sb.toString();
}
// To get the mms..
public ArrayList<Bitmap> getMms(String mmsId) {
Bitmap bitmap = null;
ArrayList<Bitmap> arlBitmap = new ArrayList<Bitmap>();
String selectionPart = "mid=" + mmsId;
Uri uri = Uri.parse("content://mms/part");
Cursor cPart = getContentResolver().query(uri, null, selectionPart,
null, null);
if (cPart.moveToFirst()) {
do {
String partId = cPart.getString(cPart.getColumnIndex("_id"));
String type = cPart.getString(cPart.getColumnIndex("ct"));
if ("image/jpeg".equals(type) || "image/bmp".equals(type)
|| "image/gif".equals(type) || "image/jpg".equals(type)
|| "image/png".equals(type)) {
bitmap = getMmsImage(partId);
arlBitmap.add(bitmap);
}
} while (cPart.moveToNext());
return arlBitmap;
}
return arlBitmap;
}
// To get bitmap from mms
private Bitmap getMmsImage(String _id) {
Uri partURI = Uri.parse("content://mms/part/" + _id);
InputStream is = null;
Bitmap bitmap = null;
try {
is = getContentResolver().openInputStream(partURI);
bitmap = BitmapFactory.decodeStream(is);
} catch (IOException e) {
} finally {
if (is != null) {
try {
is.close();
} catch (IOException e) {
}
}
}
return bitmap;
}
}
Please help me.