标签:
今天做了一个有关ContentProvider的短信备份的小案例,遇到短信备份失败,费了一番周折后终于找到了问题所在
该案例是将短信写到一个xml文件然后保存在手机存储中实现短信的备份功能,关键实现代码如下
public class SmsUtils { public static void backUpSms(List<SmsInfo> smsInfos, Context context) { try { XmlSerializer serializer = Xml.newSerializer(); File file = new File(Environment.getExternalStorageDirectory(), "sms.xml"); //初始化序列化器,指定xml数据写入到哪个文件,并且指定文件的编码方式 FileOutputStream os = new FileOutputStream(file); serializer.setOutput(os, "utf-8"); serializer.startDocument("utf-8", true); //构建根节点 serializer.startTag(null, "smss"); for (SmsInfo info : smsInfos) { //构建父节点开始号标签 serializer.startTag(null, "sms"); serializer.attribute(null, "id", info.getId() + ""); //构建子节点body serializer.startTag(null, "body"); serializer.text(info.getBody()); serializer.endTag(null, "body"); //构建子节点address serializer.startTag(null, "address"); serializer.text(info.getAddress()); serializer.endTag(null, "address"); //构建子节点type serializer.startTag(null, "type"); serializer.text(info.getType() + ""); serializer.endTag(null, "type"); //构建子节点date serializer.startTag(null, "date"); serializer.text(info.getDate() + ""); serializer.endTag(null, "date"); //父节点结束标签 serializer.endTag(null, "sms"); } serializer.endTag(null, "smss"); serializer.endDocument(); os.close(); Toast.makeText(context, "备份成功", Toast.LENGTH_LONG).show(); } catch (Exception e) { e.printStackTrace(); Toast.makeText(context, "备份失败", Toast.LENGTH_LONG).show(); } } }
下面是Logcat打印的信息
07-13 16:44:18.195 26990-26990/wp.contentresolver D/StubController: holdAndGetPermissionType permissionType:4 uid:10382 pid:26990 07-13 16:44:18.195 26990-26990/wp.contentresolver D/StubController: addRequestCount, mRequestCount =1 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 4 07-13 16:44:18.195 26990-26990/wp.contentresolver D/StubController: holdForGetPermissionSelection mRequestCount:1 07-13 16:44:18.200 26990-26990/wp.contentresolver D/StubController: beforeShowDialogCheckResult:1 07-13 16:44:18.200 26990-26990/wp.contentresolver D/StubController: minusRequestCount, mRequestCount =0 mPhoneIDRequestCount: 0 mLocationRequestCount: 0 permissionType is: 4 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: java.lang.IllegalArgumentException: Illegal character (d83d) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at org.kxml2.io.KXmlSerializer.reportInvalidCharacter(KXmlSerializer.java:144) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at org.kxml2.io.KXmlSerializer.writeEscaped(KXmlSerializer.java:130) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at org.kxml2.io.KXmlSerializer.text(KXmlSerializer.java:536) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at wp.contentresolver.SmsUtils.backUpSms(SmsUtils.java:56) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at wp.contentresolver.MainActivity.click(MainActivity.java:36) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at java.lang.reflect.Method.invoke(Method.java:515) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.view.View.performClick(View.java:4446) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.view.View$PerformClick.run(View.java:18480) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.os.Handler.handleCallback(Handler.java:733) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.os.Looper.loop(Looper.java:136) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5315) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at java.lang.reflect.Method.invokeNative(Native Method) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at java.lang.reflect.Method.invoke(Method.java:515) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:864) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:680) 07-13 16:44:18.220 26990-26990/wp.contentresolver W/System.err: at dalvik.system.NativeStart.main(Native Method)
由于Logcat上没有明显的错误信息,盲目的在真机上测试了多遍,都是以失败告终,代码检查了多遍也都没有问题,于是感觉是手机系统的问题,毕竟前面有过这样的情况,
就拿同学的手机测试了一下,结果显示备份成功了,猜想是正确的吗? --当然不是,看着Logcat上的错误信息,继续了探究,询问了导员后,开始了错误的排查,问题最终就是定位在将短信写入xml上,
首先将增强for循环去掉,只写了一个简单的头尾节点,结果成功了,又依次增加了几个子节点,将text写成固定的内容,运行起来备份又成功了,因此,可以知道自己的真机可以正常的写xml文件
又将节点中text属性固定的内容改成了动态的读取短信的内容(serializer.text(smsInfos.get(0).getBody());),这样真机也可以正常的读取短信内容并可以将其写入xml中,
最后就是for循环的问题,因为写单独的节点可以正常读取,而读取全部信息就失败,所以可以说明是其中某个信息有问题,仔细查看了一下真机上的信息,发现里面有表情,于是将所有带表情的信息都删除了,
又重新运行了一下备份,结果成功了,问题终于被解决了,不是手机系统的问题,原来是编码的转换问题,短信中的那种表情(自带的除外)不能正常的转换utf-8格式字符,导致备份失败,后来又结合了错误日志,
上面报了一个java.lang.IllegalArgumentException: Illegal character (d83d)的信息,意思是非法参数异常,下面还在body节点处报了一个错误,所以可以得出,是信息内容的问题
所以,以后一定要仔细查看Logcat错误信息并逐一排查错误,不被表面现象迷惑
标签:
原文地址:http://www.cnblogs.com/cxsy/p/5668046.html