从未读彩信中获取附件

发布于 2024-11-30 08:39:15 字数 3416 浏览 0 评论 0原文

我想从未读彩信中获取附件,但我拥有的代码不允许我这样做。我该如何去做呢?

代码修改自 这里

private void checkMMSMessages(){ 

    // Create string arrays to store the queries later on
    String[] columns = null; 
    String[] values = null; 

    // Calls the ContentResolver to query for columns with URI "content:mms"
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, null, null, null); 
    if(curPdu.moveToNext()){ 
        //String read = curRead.getString(curRead.getColumnIndex("read"));
        // Gets ID of message
        String id = curPdu.getString(curPdu.getColumnIndex("_id")); 
        // Gets thread ID of message
        String thread_id = curPdu.getString(curPdu.getColumnIndex("thread_id")); 
        // Gets subject of message (if any)
        String subject = curPdu.getString(curPdu.getColumnIndex("sub")); 
        // Gets date of message
        String date = curPdu.getString(curPdu.getColumnIndex("date")); 

        String selectionAddr = new String ("msg_id = '" + id + "'"); 
        Uri uriAddr = Uri.parse ("content://mms/" + id + "/addr"); 
        Cursor curAddr = getContentResolver().query(uriAddr, null, null, null, null); 
        if(curAddr.moveToNext()){ 
            String contact_id = curAddr.getString (curAddr.getColumnIndex ("contact_id")); 
            String address = curAddr.getString (curAddr.getColumnIndex ("address")); 
            String selectionPart = new String ("mid = '" + id + "'"); 
            Cursor curPart = getContentResolver ().query(Uri.parse ("content://mms/part"), null, null, null, null); 
            //Cursor curPart = context.getContentResolver ().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null); 

            while(curPart.moveToNext()) 
            { 
                columns = curPart.getColumnNames(); 
                if(values == null) 
                    values = new String[columns.length]; 

                for(int i=0; i< curPart.getColumnCount(); i++){ 
                    values[i] = curPart.getString(i); 
                } 
                String contact_idd = curPart.getString(0); 

                if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || 
                        values[3].equals("image/gif") || values[3].equals("image/jpg") ||
                        values[3].equals("image/png")) 
                { 
                    GetMmsAttachment(values[0],values[12]); 
                    //Toast.makeText(getApplicationContext(), "Retrieved MMS attachment", Toast.LENGTH_LONG);
                } 
            } 
        } 
    } 
} 

private void GetMmsAttachment(String _id, String _data) 
{ 
    Uri partURI = Uri.parse("content://mms/part/" + _id ); 
    String filePath = "/sdcard/photo.jpg";
    InputStream is = null;
    OutputStream picFile = null;
    Bitmap bitmap = null;

    try { 
        is = getContentResolver().openInputStream(partURI); 
        bitmap = BitmapFactory.decodeStream(is);

        picFile = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, picFile);
        picFile.flush();
        picFile.close();
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
        //throw new MmsException(e); 
    } 
}

I would like to get attachment from unread MMS messages, but the codes I have doesn't allow me to do so. How do I go about doing that?

Codes modified from here:

private void checkMMSMessages(){ 

    // Create string arrays to store the queries later on
    String[] columns = null; 
    String[] values = null; 

    // Calls the ContentResolver to query for columns with URI "content:mms"
    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, null, null, null); 
    if(curPdu.moveToNext()){ 
        //String read = curRead.getString(curRead.getColumnIndex("read"));
        // Gets ID of message
        String id = curPdu.getString(curPdu.getColumnIndex("_id")); 
        // Gets thread ID of message
        String thread_id = curPdu.getString(curPdu.getColumnIndex("thread_id")); 
        // Gets subject of message (if any)
        String subject = curPdu.getString(curPdu.getColumnIndex("sub")); 
        // Gets date of message
        String date = curPdu.getString(curPdu.getColumnIndex("date")); 

        String selectionAddr = new String ("msg_id = '" + id + "'"); 
        Uri uriAddr = Uri.parse ("content://mms/" + id + "/addr"); 
        Cursor curAddr = getContentResolver().query(uriAddr, null, null, null, null); 
        if(curAddr.moveToNext()){ 
            String contact_id = curAddr.getString (curAddr.getColumnIndex ("contact_id")); 
            String address = curAddr.getString (curAddr.getColumnIndex ("address")); 
            String selectionPart = new String ("mid = '" + id + "'"); 
            Cursor curPart = getContentResolver ().query(Uri.parse ("content://mms/part"), null, null, null, null); 
            //Cursor curPart = context.getContentResolver ().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null); 

            while(curPart.moveToNext()) 
            { 
                columns = curPart.getColumnNames(); 
                if(values == null) 
                    values = new String[columns.length]; 

                for(int i=0; i< curPart.getColumnCount(); i++){ 
                    values[i] = curPart.getString(i); 
                } 
                String contact_idd = curPart.getString(0); 

                if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || 
                        values[3].equals("image/gif") || values[3].equals("image/jpg") ||
                        values[3].equals("image/png")) 
                { 
                    GetMmsAttachment(values[0],values[12]); 
                    //Toast.makeText(getApplicationContext(), "Retrieved MMS attachment", Toast.LENGTH_LONG);
                } 
            } 
        } 
    } 
} 

private void GetMmsAttachment(String _id, String _data) 
{ 
    Uri partURI = Uri.parse("content://mms/part/" + _id ); 
    String filePath = "/sdcard/photo.jpg";
    InputStream is = null;
    OutputStream picFile = null;
    Bitmap bitmap = null;

    try { 
        is = getContentResolver().openInputStream(partURI); 
        bitmap = BitmapFactory.decodeStream(is);

        picFile = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, picFile);
        picFile.flush();
        picFile.close();
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
        //throw new MmsException(e); 
    } 
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

记忆之渊 2024-12-07 08:39:15

自己摸索了一下,代码如下:

private void checkMMSMessages() {

    String[] columns = null; 
    String[] values = null;
    String read = "read = 0";

    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, read, null, null); 
    if(curPdu.moveToNext()){
        String id = curPdu.getString(curPdu.getColumnIndex("_id"));
        Cursor curPart = getContentResolver().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null);

        while(curPart.moveToNext()) 
        { 
            columns = curPart.getColumnNames(); 
            if(values == null) 
                values = new String[columns.length]; 

            for(int i=0; i< curPart.getColumnCount(); i++){ 
                values[i] = curPart.getString(i); 
            } 

            if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || 
                    values[3].equals("image/gif") || values[3].equals("image/jpg") ||
                    values[3].equals("image/png")) 
            { 
                GetMmsAttachment(values[0],values[12]); 
            } 
        } 
    }
}

private void GetMmsAttachment(String _id, String _data) 
{ 
    Uri partURI = Uri.parse("content://mms/part/" + _id ); 
    String filePath = "/sdcard/photo.jpg";
    InputStream is = null;
    OutputStream picFile = null;
    Bitmap bitmap = null;

    try { 
        is = getContentResolver().openInputStream(partURI); 
        bitmap = BitmapFactory.decodeStream(is);

        picFile = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, picFile);
        picFile.flush();
        picFile.close();
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
        //throw new MmsException(e); 
    } 
}

Figured out myself, the codes are as follows:

private void checkMMSMessages() {

    String[] columns = null; 
    String[] values = null;
    String read = "read = 0";

    Cursor curPdu = getContentResolver().query(Uri.parse("content://mms"), null, read, null, null); 
    if(curPdu.moveToNext()){
        String id = curPdu.getString(curPdu.getColumnIndex("_id"));
        Cursor curPart = getContentResolver().query(Uri.parse ("content://mms/" + id + "/part"), null, null, null, null);

        while(curPart.moveToNext()) 
        { 
            columns = curPart.getColumnNames(); 
            if(values == null) 
                values = new String[columns.length]; 

            for(int i=0; i< curPart.getColumnCount(); i++){ 
                values[i] = curPart.getString(i); 
            } 

            if(values[3].equals("image/jpeg") || values[3].equals("image/bmp") || 
                    values[3].equals("image/gif") || values[3].equals("image/jpg") ||
                    values[3].equals("image/png")) 
            { 
                GetMmsAttachment(values[0],values[12]); 
            } 
        } 
    }
}

private void GetMmsAttachment(String _id, String _data) 
{ 
    Uri partURI = Uri.parse("content://mms/part/" + _id ); 
    String filePath = "/sdcard/photo.jpg";
    InputStream is = null;
    OutputStream picFile = null;
    Bitmap bitmap = null;

    try { 
        is = getContentResolver().openInputStream(partURI); 
        bitmap = BitmapFactory.decodeStream(is);

        picFile = new FileOutputStream(filePath);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 50, picFile);
        picFile.flush();
        picFile.close();
    } 
    catch (Exception e) 
    { 
        e.printStackTrace(); 
        //throw new MmsException(e); 
    } 
}
帅哥哥的热头脑 2024-12-07 08:39:15

我认为他问如何从服务器检索附件,因为它写为未读彩信...如果您有 ct_l 列,如何从该互联网地址获取数据?

I think he asked how to retrieve the attachment from the server, as it is written UNREAD mms... If you have the column ct_l how to get the data from that internet address?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文