xiaoguozi's Blog
Pay it forword - 我并不觉的自豪,我所尝试的事情都失败了······习惯原本生活的人不容易改变,就算现状很糟,他们也很难改变,在过程中,他们还是放弃了······他们一放弃,大家就都是输家······让爱传出去,很困难,也无法预料,人们需要更细心的观察别人,要随时注意才能保护别人,因为他们未必知道自己要什么·····

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

 Uri uri = data.getData();
                if (uri != null)
                {
                    mFilePath = new URIUtils().getPathFromUri(uri);
                }
解决办法如注释中的所示。 4.0以上平台会自动关闭cursor
protected String getPath(Uri uri)
    {
        String filePath = "";

        String[] projection = {MediaColumns.DATA };
        Cursor cursor = managedQuery(uri,
            projection,
            
null,
            
null,
            
null);

        
if (cursor != null)
        {
            
int columnIndex = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
            cursor.moveToFirst();
            filePath = cursor.getString(columnIndex);
            
try
            {
                
//4.0以上的版本会自动关闭 (4.0--14;; 4.0.3--15)
                if(Integer.parseInt(Build.VERSION.SDK) < 14)
                {
                    cursor.close();
                }
            }catch(Exception e)
            {
                Log.error(TAG, "error:" + e);
            }
        }

        
return filePath;
    }

posted on 2012-08-31 15:16 小果子 阅读(1046) 评论(1)  编辑 收藏 引用 所属分类: Android & Ios

FeedBack:
# re: android 4.0 以上平台选择图片报错Attempted to access a cursor after it has been closed.
2012-11-21 11:51 | 小u
为什么在4.0.3代码中找不到getPath(Uri uri)方法  回复  更多评论
  

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理