標籤:
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的正確檔案路徑的辦法