首頁(yè) 收藏 QQ群
 網(wǎng)站導(dǎo)航

ZNDS智能電視網(wǎng) 推薦當(dāng)貝市場(chǎng)

TV應(yīng)用下載 / 資源分享區(qū)

軟件下載 | 游戲 | 討論 | 電視計(jì)算器

綜合交流 / 評(píng)測(cè) / 活動(dòng)區(qū)

交流區(qū) | 測(cè)硬件 | 網(wǎng)站活動(dòng) | Z幣中心

新手入門 / 進(jìn)階 / 社區(qū)互助

新手 | 你問(wèn)我答 | 免費(fèi)刷機(jī)救磚 | ROM固件

查看: 12062|回復(fù): 0
上一主題 下一主題
[教程]

Android讀取彩信附件

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
發(fā)表于 2013-8-28 16:27 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
本帖最后由 hiand 于 2011-9-7 13:07 編輯       代碼主要實(shí)現(xiàn)一個(gè)讀取彩信列表的功能,原本功能是點(diǎn)擊某條彩信記錄,顯示相關(guān)包括文本、圖片(該處并沒(méi)有對(duì)音頻附件處理)等,該處就把他們整合到一起了。   
  1. public class SmsPage extends ListActivity{   
          
        private final String TAG="SmsPage";   
          
        private final Uri CONTENT_URI = Uri.parse("content://mms/inbox"); //查詢彩信收件箱   
          
        private final Uri CONTENT_URI_PART = Uri.parse("content://mms/part"); //彩信附件表   
          
        public void onCreate(Bundle savedInstanceState){   
            super.onCreate(savedInstanceState);   
            setContentView(R.layout.smslist);      
            Cursor cursor = getContentResolver().query(CONTENT_URI, null, null,null, null);   
            if(cursor.getCount()>0){   
                MyAdapter adapter = new MyAdapter(this,R.layout.smsitem,cursor,new String[]{},new int[]{});   
                setListAdapter(adapter);   
            }   
        }   
          
        public class MyAdapter extends SimpleCursorAdapter{   
            private String name="";   
            public MyAdapter(Context context, int layout, Cursor c, String[] from,   
                    int[] to) {   
                super(context, layout, c, from, to);   
            }   
               
            @Override   
            public void bindView(View view, Context context, Cursor cursor) {   
                TextView address = (TextView)view.findViewById(R.id.sms_address);   
                TextView body = (TextView)view.findViewById(R.id.sms_body);   
                TextView date = (TextView)view.findViewById(R.id.sms_date);   
                TextView sub = (TextView)view.findViewById(R.id.sms_sub);   
                ImageView image = (ImageView)view.findViewById(R.id.sms_image);   
       
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");   
                Date time=new Date(cursor.getLong(cursor.getColumnIndex("date"))*1000);//彩信時(shí)間   
                int id = cursor.getInt(cursor.getColumnIndex("_id"));//彩信Id   
                String subject = cursor.getString(cursor.getColumnIndex("sub"));//彩信主題   
                Cursor cAdd =null;   
                Cursor cPhones=null;   
                Cursor cPeople=null;   
                Cursor cPart=null;   
                try {   
                    //根據(jù)彩信id從addr表中查出發(fā)送人電話號(hào)碼,其中外鍵msg_id映射pdu表的id;   
                    String selectionAdd = new String("msg_id=" + id + ");   
                    Uri uriAddr = Uri.parse("content://mms/" + id + "/addr");   
                    cAdd = getContentResolver().query(uriAddr, null, selectionAdd, null, null);   
                      
                    //根據(jù)addr表中的電話號(hào)碼在phones表中查出PERSON_ID,外鍵PERSON_ID映射people表中的_id   
                    if(cAdd.moveToFirst()){//該處會(huì)得到2條記錄,第一條記錄為發(fā)件人號(hào)碼,第二條為本機(jī)號(hào)碼   
                        String number= cAdd.getString(cAdd.getColumnIndex("address"));   
                        cPhones = getContentResolver().query(Contacts.Phones.CONTENT_URI, new String[]{Contacts.Phones.PERSON_ID},Contacts.Phones.NUMBER +"=?",new String[]{number}, null);   
                        if(cPhones.getCount()>0){//根據(jù)phones表中的PERSON_ID查出 people 表中聯(lián)系人的名字   
                            while (cPhones.moveToNext()) {   
                                String pId = cPhones.getString(cPhones.getColumnIndex(Contacts.Phones.PERSON_ID));   
                                Uri uriPeo = Uri.parse(Contacts.People.CONTENT_URI+"/"+pId);   
                                cPeople = getContentResolver().query(uriPeo, null,null,null, null);   
                                if(cPeople.getCount()>0){   
                                    String str="";   
                                    while (cPeople.moveToNext()) {   
                                        if(str == ""){   
                                            str = cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));   
                                        }else{   
                                            str += ","+cPeople.getString(cPeople.getColumnIndex(Contacts.People.DISPLAY_NAME));   
                                        }   
                                    }   
                                    name=number+"/"+str;//如果通訊錄中存在,則以 ‘電話號(hào)碼/名字’ 形式顯示   
                                }else{   
                                    name=number;//如果是陌生人直接顯示電話號(hào)碼   
                                }   
                            }   
                        }else{   
                            name=number;//如果是陌生人直接顯示電話號(hào)碼   
                        }      
                    }   
                      
                    //根據(jù)彩信ID查詢彩信的附件   
                    String selectionPart = new String("mid="+id);//part表中的mid外鍵為pdu表中的_id   
                    cPart = getContentResolver().query(CONTENT_URI_PART, null,selectionPart,null, null);               
                    String bodyStr="";   
                    String[] coloumns = null;   
                    String[] values = null;   
                    while(cPart.moveToNext()){   
                        coloumns = cPart.getColumnNames();   
                        if(values == null)   
                            values = new String[coloumns.length];   
                        for(int i=0; i< cPart.getColumnCount(); i++){   
                            values[i] = cPart.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")){  //判斷附件類型   
                            image.setImageBitmap(getMmsImage(values[0]);//該處只會(huì)顯示一張圖片,如果有需求的朋友可以根據(jù)自己的需求將ImageView換成Gallery,修改一下方法   
                            image.setVisibility(View.VISIBLE);   
                        }else if(values[3].equals("text/plain")){   
                            /**該處詳細(xì)描述一下   
                            *發(fā)現(xiàn)一個(gè)版本問(wèn)題,之前用的2.1版本的多臺(tái)手機(jī)測(cè)試通過(guò),結(jié)果用1.6的G2報(bào)異常   
                            *經(jīng)過(guò)調(diào)試發(fā)現(xiàn),1.6版本part表中根本就沒(méi)有"text"列,也就是values[13],所以就   
                            *報(bào)錯(cuò)了,好像在1.6版本(我只測(cè)過(guò)G2,嘿嘿),就算是文本信息也是以一個(gè)附件形   
                            *式存在_date里面也就是values[12]里面,與圖片類似,但在2.1里面卻不是這樣存   
                            *的,文本信息被存在了"text"這個(gè)字段中,且"_date"為null*/   
       
                            if(values[12]!=null){//所以該處需判斷一下,如果_date為null,可直接設(shè)置內(nèi)容為"text"   
                                bodyStr=getMmsText(values[0]);   
                            }else{   
                                bodyStr = values[13];   
                            }   
                        }   
                    }   
                    if(!"".equals(subject) && subject != null){   
                        try {   
                            sub.setText(new String(subject.getBytes("iso-8859-1"),"UTF-8"));//設(shè)置彩信主題的編碼格式   
                        } catch (UnsupportedEncodingException e) {   
                            e.printStackTrace();   
                        }   
                    }   
                    if(!"".equals(bodyStr)){   
                        body.setText(bodyStr);   
                    }   
                    address.setText(name);   
                    date.setText(format.format(time));   
                } catch (RuntimeException e) {   
                    Log.e(TAG, e.getMessage());   
                }finally{   
                    if(cAdd != null){   
                        cAdd.close();   
                    }   
                    if(cPart != null){   
                        cPart.close();   
                    }   
                    if(cPeople != null){   
                        cPeople.close();   
                    }   
                    if(cPhones != null){   
                        cPhones.close();   
                    }   
                }   
                super.bindView(view, context, cursor);   
            }   
        }   
        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){   
                    BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));   
                    String temp = reader.readLine();   
                    while (temp != null) {   
                        sb.append(temp);   
                        temp=reader.readLine();//在網(wǎng)上看到很多把InputStream轉(zhuǎn)成string的文章,沒(méi)有這關(guān)鍵的一句,幾乎千遍一律的復(fù)制粘貼,該處如果不加上這句的話是會(huì)內(nèi)存溢出的   
                    }   
                }   
            }catch (IOException e) {   
                e.printStackTrace();     
                Log.v(TAG, "讀取附件異常"+e.getMessage());   
            }finally{   
                if (is != null){   
                    try {   
                        is.close();   
                    }catch (IOException e){   
                        Log.v(TAG, "讀取附件異常"+e.getMessage());   
                    }   
                }   
            }   
            return sb.toString();   
        }   
        private Bitmap getMmsImage(String _id){ //讀取圖片附件   
            Uri partURI = Uri.parse("content://mms/part/" + _id );   
            //ByteArrayOutputStream baos = new ByteArrayOutputStream();   
            InputStream is = null;   
            Bitmap bitmap=null;   
            try {   
                is = getContentResolver().openInputStream(partURI);   
                //byte[] buffer = new byte[256];     
                //int len = -1;   
                //while ((len = is.read(buffer)) != -1) {   
                //    baos.write(buffer, 0, len);   
                //}   
                //bitmap = BitmapFactory.decodeByteArray(baos.toByteArray(), 0, baos.toByteArray().length);   
               bitmap = BitmapFactory.decodeStream(is);   
         }catch (IOException e) {   
                e.printStackTrace();     
                Log.v(TAG, "讀取圖片異常"+e.getMessage());   
            }finally{   
                if (is != null){   
                    try {   
                        is.close();   
                    }catch (IOException e){   
                        Log.v(TAG, "讀取圖片異常"+e.getMessage());   
                    }   
                }   
            }   
            return bitmap;   
        }   
    }
復(fù)制代碼
</div

上一篇:Android游戲開(kāi)發(fā)之處理按鍵的響應(yīng)方式(二十二)
下一篇:Android游戲開(kāi)發(fā)之單點(diǎn)觸摸與多點(diǎn)觸摸的響應(yīng)方式(二十三)

本版積分規(guī)則

Archiver|新帖|標(biāo)簽|軟件|Sitemap|ZNDS智能電視網(wǎng) ( 蘇ICP備2023012627號(hào) )

網(wǎng)絡(luò)信息服務(wù)信用承諾書 | 增值電信業(yè)務(wù)經(jīng)營(yíng)許可證:蘇B2-20221768 丨 蘇公網(wǎng)安備 32011402011373號(hào)

GMT+8, 2024-10-22 12:39 , Processed in 0.069557 second(s), 14 queries , Redis On.

Powered by Discuz!

監(jiān)督舉報(bào):report#znds.com (請(qǐng)將#替換為@)

© 2007-2024 ZNDS.Com

快速回復(fù) 返回頂部 返回列表