首页 > 移动平台 > 详细

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

时间:2015-11-16 14:08:19      阅读:514      评论:0      收藏:0      [点我收藏+]
private String getRealPath( Uri fileUrl ) {
        String fileName = null;
        Uri filePathUri = fileUrl;
        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( ) ) {
                    int column_index = cursor.getColumnIndexOrThrow( MediaStore.Images.Media.DATA );
                    fileName = cursor.getString( column_index ); // 取出文件路径
                    if( !fileName.startsWith( "/mnt" ) ) {
                        // 检查是否有”/mnt“前缀

                        fileName = "/mnt" + fileName;
                    }
                    cursor.close( );
                }
            } else if( fileUrl.getScheme( ).compareTo( "file" ) == 0 ) // file:///开头的uri
            {
                fileName = filePathUri.toString( );
                fileName = filePathUri.toString( ).replace( "file://", "" );
                // 替换file://
                if( !fileName.startsWith( "/mnt" ) ) {
                    // 加上"/mnt"头
                    fileName += "/mnt";
                }
            }
        }
        return fileName;
    }

 

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

原文:http://www.cnblogs.com/androidxiaoyang/p/4968663.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!