从MMS获取图像会产生错误的彩信

时间:2014-11-24 10:43:26

标签: android

我的应用正在尝试从设备中提取短信和彩信,并将其存储在数据库中。我试过这段代码:How to Read MMS Data in Android?这个工作正常,但问题是得到了错误的彩信图片。这种情况发生在我发送新彩信的情况下,同时备份彩信。 这是我的代码:

// 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;
    }
}

请帮帮我。

1 个答案:

答案 0 :(得分:0)

一次运行两个线程,一个是备份映像,第二个是从sqlite检索映像。更改两个文件夹不同可以消除此问题