1

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.

Community
  • 1
  • 1
syam vakkalanka
  • 426
  • 3
  • 15
  • Can you give an example as to which MMS you retrieve and which one you actually wanted? (The desired outcome vs. the current outcome.) – MarchingHome Nov 24 '14 at 10:47
  • if i send mms when i retrieving mms images from sqlite that mms is replaces with mms image which i was send last month or before moth .if i run my app and try same way it replaces with one at a time not constantly single one it changes every time – syam vakkalanka Nov 24 '14 at 12:17

1 Answers1

0

Running two threads at a time one is Backup images and second one is retrieving images from sqlite . Change two folders different can eliminate this problem

syam vakkalanka
  • 426
  • 3
  • 15