android 获取uri的正确文件路径的办法

private String getRealPath( Uri fileUrl ) {
        String fileName = null;
        if( fileUrl != null ) {
            if( fileUrl.getScheme( ).toString( ).compareTo( "content" ) == 0 ) // content://开头的uri
            {
                Cursor cursor = this.getContentResolver( ).query( fileUrl, null, null, null, null );
                if( cursor != null && cursor.moveToFirst( ) ) {
                    try {
                        int column_index = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
                        fileName = cursor.getString( column_index ); // 取出文件路径
                    } catch( IllegalArgumentException e ) {
                        e.printStackTrace();
                    }finally{
                        cursor.close( );
                    }
                }
            } else if( fileUrl.getScheme( ).compareTo( "file" ) == 0 ) // file:///开头的uri
            {
                fileName = fileUrl.getPath( );
            }
        }
        return fileName;
    }

网上搜的好坑,自己修改了一下。

转载于:https://www.cnblogs.com/androidxiaoyang/p/4968663.html

你可能感兴趣的:(android 获取uri的正确文件路径的办法)