android 4.0 以上平台选择图片报错Attempted to access a cursor after it has been closed.

android 4.0 以上平台选择图片报错Attempted to access a cursor after it has been closed.

选择图片后 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;
    }

你可能感兴趣的:(android 4.0 以上平台选择图片报错Attempted to access a cursor after it has been closed.)