博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Attempted to access a cursor after it has been clo
阅读量:6971 次
发布时间:2019-06-27

本文共 1612 字,大约阅读时间需要 5 分钟。

  hot3.png

[html] 
  1. android.database.StaleDataException: Attempted to access a cursor after it has been closed.  
  2. at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2444)  
  3. at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2472)  
  4. at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1173)  
  5. at android.os.Handler.dispatchMessage(Handler.java:99)  
  6. at android.os.Looper.loop(Looper.java:137)  
  7. at android.app.ActivityThread.main(ActivityThread.java:4424)  
  8. at java.lang.reflect.Method.invokeNative(Native Method)  
  9. at java.lang.reflect.Method.invoke(Method.java:511)  
  10. at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)  
  11. at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)  
  12. at dalvik.system.NativeStart.main(Native Method)  
  13. Caused by: android.database.StaleDataException: Attempted to access a cursor after it has been closed.  

选择图片后 onActivityResult中的代码如下:

[java] 
  1. Uri uri = data.getData();  
  2.                if (uri != null)  
  3.                {  
  4.                    mFilePath = new URIUtils().getPathFromUri(uri);  
  5.                }  
解决办法如注释中的所示。 4.0以上平台会自动关闭cursor
[java] 
  1. protected String getPath(Uri uri)  
  2.     {  
  3.         String filePath = "";  
  4.   
  5.         String[] projection = {MediaColumns.DATA };  
  6.         Cursor cursor = managedQuery(uri,  
  7.             projection,  
  8.             null,  
  9.             null,  
  10.             null);  
  11.   
  12.         if (cursor != null)  
  13.         {  
  14.             int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);  
  15.             cursor.moveToFirst();  
  16.             filePath = cursor.getString(columnIndex);  
  17.             try  
  18.             {  
  19.                 //4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)  
  20.                 if(Integer.parseInt(Build.VERSION.SDK) < 14)  
  21.                 {  
  22.                     cursor.close();  
  23.                 }  
  24.             }catch(Exception e)  
  25.             {  
  26.                 Log.error(TAG, "error:" + e);  
  27.             }  
  28.         }  
  29.   
  30.         return filePath;  
  31.     }  

转载于:https://my.oschina.net/macleo/blog/200033

你可能感兴趣的文章
寒冰linux视频教程笔记4 目录
查看>>
zabbix中通过shell脚本进行微信监控告警
查看>>
Windows下Nginx的启动、停止等命令
查看>>
我的友情链接
查看>>
sed运用实例一——基于变量的动态替换
查看>>
centos下搭建svn的安装过程
查看>>
Html 常见问题
查看>>
网站安全检测之图片验证码
查看>>
PB 级数据处理挑战,Kubernetes如何助力基因分析?
查看>>
vue cle新建vue项目
查看>>
配置网络、远程登录
查看>>
阿里架构师干货分享——eureka分布式框架demo
查看>>
GitHub十周岁HanLP自然语言处理包用户量超越CoreNLP
查看>>
Pycharm上Django的使用 Day2
查看>>
5.22-zabbix监控Nginx
查看>>
JVM基础系列开篇:为什么要学虚拟机?
查看>>
unicode字符串转list的方法
查看>>
OSChina 周五乱弹 —— 草莓味的唇膏是什么味道?
查看>>
OSChina 周三乱弹 ——纪念Bob Taylor
查看>>
OSChina 周一乱弹 ——爱丽三个小时没吃鱼罐头了
查看>>